3

I'm using a library that has a function, f. This function accepts a few arguments: an object, a dataframe, and the name of a column in the dataframe. If I call it manually, it works without any trouble. I call it like this:

f(my_object, my_dataframe, 'A')

However, if I put 'A' in a variable, it doesn't work! To clarify, I just do this:

g = 'A'    
f(my_object, my_dataframe, g)

And I get an error (undefined columns selected). I've tried googling to figure this out, but no luck. If anyone could help I would really appreciate it.


EDIT: I'm using the partialPlot command in the randomForest library. Here's exactly what I'm typing:

partialPlot(r,x,'pH')

This works! Next, I assign 'pH' to a variable and try the exact same function:

g = 'pH'    
partialPlot(r,x,g)

This doesn't work and I get the following error:

Error in '[.data.frame'(pred.data, , xname) : undefined columns selected

I can also verify that g is what I think it is:

print(g)
#[1] "pH"

class(g)
#[1] "character"
Boxuan
  • 4,937
  • 6
  • 37
  • 73
a b
  • 171
  • 1
  • 1
  • 10
  • Are you sure you're passing in `"A"` to `f` and not just `A`? – Scott Ritchie Sep 22 '13 at 01:01
  • 1
    What library are you talking about? – unique2 Sep 22 '13 at 01:01
  • 2
    It could be a similar *interactive* function to `subset`, see http://stackoverflow.com/q/9860090/1201032 – flodel Sep 22 '13 at 01:05
  • I added some specifics to the original post - hope this helps. flodel, I looked at the link. I get the general idea, but the details are over my head. If it's an interactive function, is there anything I can do? – a b Sep 22 '13 at 01:16
  • Possible duplicate of [What causes this weird behaviour in the randomForest.partialPlot function?](http://stackoverflow.com/questions/32750984/what-causes-this-weird-behaviour-in-the-randomforest-partialplot-function) – Boxuan Apr 27 '17 at 17:01
  • I tried using `do.call` to call `partialPlot` according to [this post](http://stackoverflow.com/questions/32750984/what-causes-this-weird-behaviour-in-the-randomforest-partialplot-function), and it works for me. – Boxuan Apr 27 '17 at 17:02

2 Answers2

3

Try

g = quote(pH)
partialPlot(r,x,g)

The culprit is the following piece in randomForest:::partialPlot.randomForest

x.var <- substitute(x.var)
xname <- if (is.character(x.var)) 
    x.var
else {
    if (is.name(x.var)) 
        deparse(x.var)
    else {
        eval(x.var)
    }
}

For more background see stackoverflow.com/q/9860090/1201032


Earlier try (only worked interactively):

partialPlot(r,x,c(g)) should work.Writing c(g) instead of g makes is.name(x.var) return FALSE so eval instead of deparse gets executed.

Community
  • 1
  • 1
unique2
  • 2,162
  • 2
  • 18
  • 23
  • This works interactively, but for some reason, it doesn't work in my script. I get the following error: Error in eval(expr, envir, enclos) : object 'g' not found . Calls: main ... partialPlot -> partialPlot.randomForest -> eval -> eval . Execution halted . Any ideas? Thanks for the help! I'll try debugging it... – a b Sep 22 '13 at 01:36
  • How do you execute your script? – unique2 Sep 22 '13 at 01:38
  • I use "Rscript ./myscript.r" followed by optparse-style arguments – a b Sep 22 '13 at 01:54
  • Oh thanks - didn't see it. I tried calling it like this: partialPlot(r,x,quote(g)), but I got the following error: Error in .subset(x, j) : invalid subscript type 'symbol' . Calls: main ... partialPlot -> partialPlot.randomForest -> [ -> [.data.frame Execution halted . – a b Sep 22 '13 at 02:13
  • Just to clarify, in my script, the name 'pH' only exists inside a variable (g). I don't know it beforehand. I am trying to run this function on 100s of such variables, so I can't manually type each one. – a b Sep 22 '13 at 02:17
  • Next try: g = as.symbol('pH') – unique2 Sep 22 '13 at 02:32
  • I tried partialPlot(r,x,as.symbol(g)). I got the following error? Error in as.symbol(g) : object 'g' not found . Calls: main ... partialPlot.randomForest -> eval -> eval -> as.symbol . But 'g' definitely exists. I can print it before I call the function. – a b Sep 22 '13 at 02:37
  • make it sym = as.symbol(g) and partialPlot(r,x,as.sym) – unique2 Sep 22 '13 at 02:42
  • Error in `[.data.frame`(pred.data, , xname) : undefined columns selected Calls: main ... partialPlot -> partialPlot.randomForest -> [ -> [.data.frame Execution halted – a b Sep 22 '13 at 03:01
  • The above error is trying, as you suggested, sym=as.symbol(g). Then in the next line, I call the function: partialPlot(r,x,as.sym). Do you know if there is an easy way to modify the code to do what I want? Sorry, I am still learning R, so I am a little over my head. Really appreciate the help. – a b Sep 22 '13 at 03:38
1

I encountered this problem myself. This is a messy solution, but it worked for me. Using eval() is considered bad programming, but the bug in partialPlot is so mind-boggling, I think desperate times call for desperate measures!

To.Eval <- paste("partialPlot(r, x, '", 
                  g, 
                  "')", 
                  sep = "")
L <- eval(parse(text = To.Eval))
Arthur
  • 1,248
  • 8
  • 14