Search code examples
rclassmethodsreference-class

Error message in R: "Arguments to methods() must be named, or one named list"


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.


Solution

  • 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).