[R] Ensure parameter is a string when passed within an lapply & called function runs a 'substitute' on it

Andrew Clancy nite at achren.org
Wed May 11 13:02:06 CEST 2016


Thanks David - my earlier response to Bert contains the resolution.
partialPlot was commented out deliberately as it was the target function
who's behaviour I was replicating in testFunc. The original behaviour, ie.
printing 'X1' was correct, and the do.call fix yields this same response
when testFunc is called within lapply. As I'm replicating partialPlot, no
changes can be made to testFunc (eg. your removal of 'substitute')
otherwise I'd need to patch the randomForest::partialPlot package &
function. The correct patch would be to change the eval to use the parent
environment, the subtitue should remain.

See the resolution here (jcheng beat r-help to it this time!)
https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/shiny-discuss/cIZJzQmw8tQ


On 11 May 2016 at 08:48, David Winsemius <dwinsemius at comcast.net> wrote:

>
> > On May 9, 2016, at 2:39 PM, Andrew Clancy <nite at 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
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list