3

I am fairly new to R, so my apologies if this question is a bit silly.

I am calling a function in an external package ('mmlcr', although I don't think that is directly relevant to my problem), and one of the required inputs (data) is a data.frame. I compose the data.frame from various data using the following approach (simplified for illustration):

    #id, Time, and value are vectors created elsewhere in the code.
    myData = data.frame(a=id, b=Time, c=value)  
    out <- mmlcr( input1, input2, data=myData, input4)

Which throws the error:

Error in is.data.frame(data) : object 'myData' not found

The debugger indicates that this error is thrown during the mmlcr() call.

I then added a print(ls()) immediately prior to the mmlcr() call, and the output confirmed that "myData" was in my function workspace; further is.data.frame(myData) returned TRUE. So it seems that "myData" is successfully being created, but for some reason it is not passing into the mmlcr() function properly. (Commenting this line causes no error to be thrown, so I'm pretty sure this is the problematic line).

However, when I put the exact same code in a script (i.e., not within a function block), no such error is thrown and the output is as expected. Thus, I assume there is some scoping issue that arises.

I have tried both assignment approaches:

myData = data.frame(a=id, b=Time, c=value)  
myData <- data.frame(a=id, b=Time, c=value)  

and both give me the same error. I admit that I don't fully understand the scope model in R (I've read about the differences between = and <- and I think I get it, but I'm not sure).

Any advice you can offer would be appreciated.

  • I think you'll need to post your function as an update to your question, but you are almost surely correct, that the problem is scoping. And let me confirm: it works as a function, but not as a script? Usually it would be the other way around. Anyway, show us a minimum working/not working example. – Bryan Hanson May 21 '13 at 17:30
  • The reason I ask is that the question title conflicts with the paragraph beginning with 'However'. – Bryan Hanson May 21 '13 at 17:37
  • The error occurs when mmlcr() is called within a function, and does not occur within the script. I'm not sure how this conflicts with the question title. – user2406446 May 21 '13 at 18:02
  • You are correct: I misread it twice! – Bryan Hanson May 21 '13 at 18:05

1 Answers1

1

MMLCR is now deprecated and you should search for some alternatives. Without looking too much into it, I sleuthed through an old repo and found the culprit:

m <- eval(m, data)

in the function mmlcr.default. There are a lot of reasons why this is bad, but scoping is the big one. R has this issue with the subset.data.frame function, see my old SO question. Rather than modify the source code, I would find a way to do your function with a subroutine using a for, repeat, or while loop.

Community
  • 1
  • 1
AdamO
  • 4,283
  • 1
  • 27
  • 39
  • Ah, looking at the mmlcr.default, I see what you mean. As I said in my question, I am new to R, but I come from a Matlab background where it is encouraged to avoid eval() at all costs, precisely because of the scoping problems therein. So I'm not surprised this line has thrown an issue. I will search for alternatives to mmlcr(), although I have had trouble finding other similar packages before. Thanks for your help! – user2406446 May 21 '13 at 19:07