I've been creating some R tools that manipulate objective functions for optimization. In so doing, I create a character string with R code, and then want to have it in my workspace. Currently -- and this works fine -- I write the code out, then use source() to bring it in again. Example: cstr<-"jack<-function(x){\n cat(\"Silly x:\")\n print(x) \n }\n" write(cstr, file='tfile.txt') jack<-source('tfile.txt')$value # You need the value element! print(jack) However, I feel it would be more elegant if I could avoid the file, and am sure I must have missed some way to pipe the cstr through the source() function. Also, if the file cannot be written (directory permissions?), then my approach won't work. JN
On Sat, Apr 28, 2012 at 10:27 AM, John C Nash <nashjc at uottawa.ca> wrote:> I've been creating some R tools that manipulate objective functions for > optimization. In so doing, I create a character string with R code, and then > want to have it in my workspace. Currently -- and this works fine -- I write > the code out, then use source() to bring it in again. Example: > > cstr<-"jack<-function(x){\n cat(\"Silly x:\")\n print(x) \n ?}\n" > write(cstr, file='tfile.txt') > jack<-source('tfile.txt')$value # You need the value element! > print(jack) > > However, I feel it would be more elegant if I could avoid the file, and am > sure I must have missed some way to pipe the cstr through the source() > function. Also, if the file cannot be written (directory permissions?), then > my approach won't work.Try this:> cstr<-"jack<-function(x){\n cat(\"Silly x:\")\n print(x) \n }\n" > eval(parse(text = cstr)) > jack(0)Silly x:[1] 0 This also works:> f <- function(x) {} > body(f) <- parse(text = '{\n cat("Silly x:")\n print(x) \n }\n') > f("X")Silly x:[1] "X" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi JN, You can use eval(parse(text = cstr)) for this. I've been told to avoid this when possible, though I'm not sure why. Best, Ista On Sat, Apr 28, 2012 at 10:27 AM, John C Nash <nashjc at uottawa.ca> wrote:> I've been creating some R tools that manipulate objective functions for > optimization. In so doing, I create a character string with R code, and then > want to have it in my workspace. Currently -- and this works fine -- I write > the code out, then use source() to bring it in again. Example: > > cstr<-"jack<-function(x){\n cat(\"Silly x:\")\n print(x) \n ?}\n" > write(cstr, file='tfile.txt') > jack<-source('tfile.txt')$value # You need the value element! > print(jack) > > However, I feel it would be more elegant if I could avoid the file, and am > sure I must have missed some way to pipe the cstr through the source() > function. Also, if the file cannot be written (directory permissions?), then > my approach won't work. > > > JN > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.