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

Bert Gunter bgunter.4567 at gmail.com
Tue May 10 20:17:03 CEST 2016


Well, here's a very bad way of doing this:

vars <- names(dataSet)

lapply(vars, function(var) {
  assign("y",var,pos=parent.frame(2))
  testFunc(x = model, pred.data = dataSet,
x.var=eval(y,parent.frame(2)), plot = F)
} )

## results in

[1] "X1"
[1] "X2"
[1] "X3"
[1] "X4"
[1] "X5"
[1] "X6"
[[1]]
[1] "X1"

[[2]]
[1] "X2"

[[3]]
[1] "X3"

[[4]]
[1] "X4"

[[5]]
[1] "X5"

[[6]]
[1] "X6"


Assigning to the calling environment is a bad idea, but I have not
figured out a better way to go about this.


Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, 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
>
> 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)
>
> testFunc <- function(x, pred.data, x.var, plot=F) {
>   x.var <- substitute(x.var)
>   # 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)
>
> lapply(vars, function(var) {
>   # print(paste('var', var))
>   testFunc(x = model, pred.data = dataSet, x.var = var, plot = F)
> })
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list