Dear users, I got really stuck trying to apply a function to a piece of code that I created using different string functions. To make things really easy, this is a wee example: x<-c(1:10) script<-"x, trim = 0, na.rm = FALSE" ##script created by a number of paste() and rep() steps mean(script) ##function that I want to apply: doesn't work Is there any way to convert the "script" class so that the function mean() can read it as if it were text typed in the console? Thanks and have a superb day Javier -- View this message in context: http://www.nabble.com/How-can-I-run-a-function-to-a-piece-of-text--tp25930315p25930315.html Sent from the R help mailing list archive at Nabble.com.
Based solely on what you told us, this can be done using eval(parse(text=...))
cmd <- sprintf("mean(%s)", script)
eval(parse(text = cmd))
However, with more context, there may be a better solution. See, for example,
install.packages("fortunes")
library(fortunes)
fortune("parse()")
HTH,
--sundar
On Fri, Oct 16, 2009 at 11:39 AM, Javier PB
<j.perez-barberia at macaulay.ac.uk> wrote:>
> Dear users,
>
> I got really stuck trying to apply a function to a piece of code that I
> created using different string functions.
>
> To make things really easy, this is a wee example:
>
> x<-c(1:10)
> script<-"x, trim = 0, na.rm = FALSE" ? ? ? ?##script created
by a number of
> paste() and rep() steps
> mean(script) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ##function that I want
to
> apply: doesn't work
>
> Is there any way to convert the "script" class so that the
function mean()
> can read it as if it were text typed in the console?
>
> Thanks and have a superb day
>
> Javier
>
>
>
> --
> View this message in context:
http://www.nabble.com/How-can-I-run-a-function-to-a-piece-of-text--tp25930315p25930315.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Javier PB wrote:> > I got really stuck trying to apply a function to a piece of code > that I created using different string functions. > > To make things really easy, this is a wee example: > > x<-c(1:10) > script<-"x, trim = 0, na.rm = FALSE" ##script created by a > number of paste() and rep() steps mean(script) > ##function that I want to apply: doesn't work > > Is there any way to convert the "script" class so that the function > mean() can read it as if it were text typed in the console? >That's a really tricky question. I think that there are two solutions, both of them pass through an alternate way of calling functions. Namely: for each function that can be called as f(x=a, y=b, z=c), you can use do.call(f, list(x=a,y=b,z=c)) or even do.call(f, list(a, b, c)). Let's test this. f <- function(x, y) x + 2 * y f(1, 2) do.call(f, list(1, 2)) So far so good. The problem now is that you don't have a list(x, trim = 0, na.rm = FALSE), but a string! But it's a piece of cake to convert a string to an R object (but only after you know the magic words, and they are eval(parse(text=string))): eval(parse(text = paste("list(", script, ")", sep = "")) So, a slow and didatical(sp?) solution to your problem is: x<-c(1:10) script<-"x, trim = 0, na.rm = FALSE" # Step 1: convert script to a list - but still as a string script.list.string <- paste("list(", script, ")", sep = "") # Step 2: convert script to a list script.list <- eval(parse(text = script.list.string)) # Step 3: call the function do.call(mean, script.list) The second way (and simpler) is to enclose the "mean" function into the script string, and then invoke the magic words: x<-c(1:10) script<-"x, trim = 0, na.rm = FALSE" # Step 1: convert script to the calling of mean - but still as a string mean.string <- paste("mean(", script, ")", sep = "") # Step 2: compute it eval(parse(text = mean.string)) Alberto Monteiro
Henrique Dallazuanna
2009-Oct-16 19:18 UTC
[R] How can I run a function to a piece of text?
An alternative could be: do.call(mean, as.list(parse(text = unlist(strsplit(script, ", "))))) On Fri, Oct 16, 2009 at 3:39 PM, Javier PB <j.perez-barberia at macaulay.ac.uk> wrote:> > Dear users, > > I got really stuck trying to apply a function to a piece of code that I > created using different string functions. > > To make things really easy, this is a wee example: > > x<-c(1:10) > script<-"x, trim = 0, na.rm = FALSE" ? ? ? ?##script created by a number of > paste() and rep() steps > mean(script) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ##function that I want to > apply: doesn't work > > Is there any way to convert the "script" class so that the function mean() > can read it as if it were text typed in the console? > > Thanks and have a superb day > > Javier > > > > -- > View this message in context: http://www.nabble.com/How-can-I-run-a-function-to-a-piece-of-text--tp25930315p25930315.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O