> On May 9, 2016, at 2:39 PM, Andrew Clancy <n...@achren.org> wrote:
> 
> Hi, 
> 
> I’m trying to solve what looks like the same issue as stack overflow article, 
> but within an lapply:
> http://stackoverflow.com/questions/18939254/cant-use-a-variable-as-an-argument-but-can-use-its-value


It would be helpful if you could articulate the issue.

> 
> I’ve replicated the issue with partialPlot below in ‘testFunc’. The lines up 
> to the final print can’t change (including the substitute). In the first call 
> it prints out ‘X1’ correctly, in the second it prints out ‘var’. I’ve tried 
> eval, quote etc as the article suggests. Any ideas?
> 
> numObs  <- 10
> numVars <- 6
> dataSet    <- data.frame(replicate(numVars,rnorm(numObs)))
> # partialPlot(x = model, pred.data = dataSet, x.var = 'X1', plot = F) 

I'm assuming that the comment character is actually something that was inserted 
in hte process of stripping hte HTML from this posting.

It throws an error when removed:

Error in partialPlot(x = model, pred.data = dataSet, x.var = "X1", plot = F) : 
  object 'model' not found

> 

> testFunc <- function(x, pred.data, x.var, plot=F) {
>   x.var <- substitute(x.var)

Try changing to eval(x.bar)

>   # print(paste('is.character(x.var)', is.character(x.var), 'is.name(x.var)', 
> is.name(x.var)))

>   xname <- if (is.character(x.var)) x.var else {
>     if (is.name(x.var)) deparse(x.var) else {
>       eval(x.var)
>     }
>   }
>   print(xname)
>   # print(head(pred.data[,xname]))
> }
> 
> vars <- names(dataSet)[[1]]
> testFunc(x = model, pred.data = dataSet, x.var = local(vars), plot = F)

Returns:
[1] "is.character(x.var) TRUE is.name(x.var) FALSE"
[1] "X1"
[1]  0.8704543 -0.4421564 -0.6725336 -1.3096399 -1.0531335 -0.4979650


> 
> lapply(vars, function(var) {
>   # print(paste('var', var))
>   testFunc(x = model, pred.data = dataSet, x.var = var, plot = F)
> })

Retruns:
[1] "var X1"
[1] "is.character(x.var) TRUE is.name(x.var) FALSE"
[1] "X1"
[1]  0.8704543 -0.4421564 -0.6725336 -1.3096399 -1.0531335 -0.4979650
[[1]]
[1]  0.8704543 -0.4421564 -0.6725336 -1.3096399 -1.0531335 -0.4979650


> 
>       [[alternative HTML version deleted]]

Please learn to post in plain text for this mailing list.

-- 

David Winsemius
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to