0

I'm new to creating classes and methods in R and am running into a problem that I haven't found much documentation on. I have created a class, 'DataImport', and am trying to add the method below:

DataImport$methods(reducedImport <- function(filePathOne, dataFrame) 
  {

  }
)

When I run this code I'm getting the following error:

Error in DataImport$methods(reducedImport <- function(filePathOne,  : 
  Arguments to methods() must be named, or one named list

I was able to add a method directly before this one and it worked fine but this one doesn't. I don't quite understand why that would be the case or how to fix it.

tjnel
  • 643
  • 1
  • 9
  • 19
  • Please read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dason Mar 27 '13 at 17:27
  • Thank you for the reply. I'm not sure I understand the suggestion though. I'm not using any dataframes or data, the only code in addition to the method seen above that I'm trying to run is the class definition, DataImport <- setRefClass("DataImport", fields = c("startDate")). This class definition and the method in my original post should constitute a class, and I'm only trying to define that class so that I can use it on a data frame and file path in the future. When I run these two blocks of code I get the error I alluded to. Am I misunderstanding the use of classes in R? – tjnel Mar 27 '13 at 17:36
  • You should include the definition of that class in your question. – Dason Mar 27 '13 at 17:44
  • 3
    And I don't work with reference classes but it seems to me that you should be using `=` instead of `<-`. That is probably what is causing the error. – Dason Mar 27 '13 at 17:45

1 Answers1

1

As Dason mentioned in the comment, your problem is with assignment. Let's create a simple example:

c1 = setRefClass("c1", fields = list( data = "numeric"))
c1$methods(m1 = function(a) a)

Now a quick test:

x = c1$new(data=10)
x$m1(1)

However,

R> c1$methods(m2 <- function(a) a)
Error in c1$methods(m2 <- function(a) a) : 
  Arguments to methods() must be named, or one named list

gives the error you see. The reason for this is that the <- operator is slightly different from the = operator. This in general doesn't matter (but it does here).

Community
  • 1
  • 1
csgillespie
  • 59,189
  • 14
  • 150
  • 185