Franklin Tamborello II
2011-Apr-07 23:45 UTC
[R] replace an expression with its value, or read macros
I know my subject line seems odd, but I want to replace an expression—such as a variable—with its value. For example, I want to paste() some strings together, assign the result to a variable s, then use the value of s as a variable to hold another value. Something like this: import_subjects <- function (start, iterations) { for (i in 1:iterations) { # iterate from start to end s <- paste("s", i, sep="") # make the data.frame's name fn <- paste(i, ".txt", sep="") # make the filename s <- read.delim(fn, header = FALSE) # now put it all together # …except that I don't want to reassign s, I want to evaluate it and make its value a variable with its own value. } } If this were Lisp I'd use backquotes (or "backticks", as some say) and commas—examples of read macros—to do this, but I can't find anything analogous for R. How might I accomplish this in R? Sincerely, Frank Tamborello, PhD W. M. Keck Postdoctoral Fellow School of Biomedical Informatics University of Texas Health Science Center at Houston [[alternative HTML version deleted]]
Thomas Lumley
2011-Apr-07 23:53 UTC
[R] replace an expression with its value, or read macros
On Fri, Apr 8, 2011 at 11:45 AM, Franklin Tamborello II <Franklin.Tamborello at uth.tmc.edu> wrote:> I know my subject line seems odd, but I want to replace an expression?such as a variable?with its value. For example, I want to paste() some strings together, assign the result to a variable s, then use the value of s as a variable to hold another value. Something like this: > > import_subjects <- function (start, iterations) { > for (i in 1:iterations) { # iterate from start to end > s <- paste("s", i, sep="") # make the data.frame's name > fn <- paste(i, ".txt", sep="") # make the filename > s <- read.delim(fn, header = FALSE) # now put it all together > # ?except that I don't want to reassign s, I want to evaluate it and make its value a variable with its own value. > } > } > > If this were Lisp I'd use backquotes (or "backticks", as some say) and commas?examples of read macros?to do this, but I can't find anything analogous for R. >How might I accomplish this in R?As a first note, most people who ask this sort of question would be better off putting the sets of data into a single list rather than named variables. However, it is possible to do what you want. One approach is to use assign(). Another is to use bquote(), which is related to the Lisp backquote macro: eval(bquote(.(s) <- read.delim(.(fn),header=FALSE)), where .() is analogous to comma. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland
Gabor Grothendieck
2011-Apr-08 00:19 UTC
[R] replace an expression with its value, or read macros
On Thu, Apr 7, 2011 at 7:45 PM, Franklin Tamborello II <Franklin.Tamborello at uth.tmc.edu> wrote:> I know my subject line seems odd, but I want to replace an expression?such as a variable?with its value. For example, I want to paste() some strings together, assign the result to a variable s, then use the value of s as a variable to hold another value. Something like this: > > import_subjects <- function (start, iterations) { > for (i in 1:iterations) { # iterate from start to end > s <- paste("s", i, sep="") # make the data.frame's name > fn <- paste(i, ".txt", sep="") # make the filename > s <- read.delim(fn, header = FALSE) # now put it all together > # ?except that I don't want to reassign s, I want to evaluate it and make its value a variable with its own value. > } > } > > If this were Lisp I'd use backquotes (or "backticks", as some say) and commas?examples of read macros?to do this, but I can't find anything analogous for R. How might I accomplish this in R? >Here are a few possibilities: # 1 - assign assign(s, read.delim(fn, header = FALSE) # 2 - .GlobalEnv .GlobalEnv[[s]] <- read.delim(fn, header = FALSE) # 3 - gsubfn's fn$ supports backticks # Here we need gsubfn::fn to disambiguate the two fn's library(gsubfn) eval(gsubfn::fn$parse(text = " `s` <- read.delim(fn, header = FALSE) ")) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com