Question

I am getting an error when I try and combine using expression with do.call and plot.

 x <- 1:10
 y <- x^1.5

I can get the plot I want by using only the plot function:

plot(y~x,xlab=expression(paste("Concentration (",mu,"M)")))

However, I would like to implement my plot using do.call. I have a really long list of parameters stored as a list, p. However, when I try and pass the list to do.call I get the following error:

p <- list(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
Error in paste("Concentration (", mu, "M)") : 
  object 'mu' not found

I also tried defining the formula explicitly in the args passed to do.call. ie. do.call(plot,c(formula=y~x,p)). I do not understand why I am getting the error - especially because the following does not give an error:

do.call(plot,c(0,p))

(and gives the desired mu character in the xaxis).

Was it helpful?

Solution 2

do.call evaluates the parameters before running the function; try wrapping the expression in quote:

p <- list(xlab=quote(expression(paste("Concentration (",mu,"M)"))))
do.call("plot", c(y~x, p))

OTHER TIPS

You can use alist rather then list

p <- alist(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))

Setting quote=TRUE also works. It in effect prevents do.call() from evaluating the elements of args before it passes them to the function given by what.

x <- 1:10
y <- x^1.5
p <- list(xlab=expression(paste("Concentration (",mu,"M)",sep="")))

do.call(what = "plot", args = c(y ~ x, p), quote = TRUE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top