问题
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).
回答1:
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))
回答2:
You can use alist
rather then list
p <- alist(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
回答3:
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)
来源:https://stackoverflow.com/questions/18280944/how-to-combine-do-call-plot-and-expression