From bo.werth at gmail.com  Fri Oct  9 11:55:08 2015
From: bo.werth at gmail.com (Bo Werth)
Date: Fri, 9 Oct 2015 11:55:08 +0200
Subject: [Rd] reverse object creation
Message-ID: <CAHaz_Kk6WkTCADXdh5A+o57iMKtVAKjv8_ROX61=TwAsW9fAHA@mail.gmail.com>

Dear all,

this is my first message to this mailing list - please advise if it is not
the right place for the subject

I've been using R very intensively the last 3-4 years and one of the most
tedious tasks is modification of lookup or conversion tables

So far, I have not found functions that create the commands for creating
objects (vectors, data frames) based on the objects themselves - i.e. to
reverse-engineer them.

Here are my suggestions:

c_rev <- function(x) cat(paste0('c("', gsub(', ', '", "', toString(x)),
'")\n'))

df_rev <- function(x) {
    X <- apply(x, 1,
               function(x) {
                   paste0('c(',
                          paste0(shQuote(x), collapse = ", "),
                          ')'
                          )
               }
               )
    command <- paste0(
        'rbind.data.frame(',
        paste0(
            X,
            collapse = ",\n"),
        ')')
    return(cat(command, "\n"))
}

Bo

---
bowerth.github.io

	[[alternative HTML version deleted]]



[Rd] reverse object creation
Bo Werth