2

I am declaring a class using:

setRefClass("XLSXFile", 
    fields = list(
        wb= "workbook", 
        sheet= "character",
        at.line= "numeric"
    )
)

The code does not compile. I get the error:

Error in .local(.Object, ...) : 
  argument "filename" is missing, with no default

The error is generated because of this line:

        wb= "workbook",

How can I declare a workbook field in a class? What function is the interpreter trying to invoke which needs the filename parameter? my guess it in loadWorkbook since I am using XLConnect but how do I supply the parameter while declaring the class?

agstudy
  • 119,832
  • 17
  • 199
  • 261

1 Answers1

0

Yes, it seems that the ref class instance is first created empty, i.e. that the fields are constructed by calling the default constructor, meaning that the error is due to:

new('workbook')
Error in .local(.Object, ...) : 
  argument "filename" is missing, with no default

You could use the ANY type:

XLSXFile <- setRefClass("XLSXFile", 
  fields = list(
    wb = "ANY",
    sheet= "character",
    at.line= "numeric"
  )
)

or maybe use a field constructor, using this type of approach:

XLSXFile <- setRefClass("XLSXFile", 
  fields = list(
    wb = function(x) { 
       message('workbook accessor/setter'); 
       # build or set the field
     },
    sheet= "character",
    at.line= "numeric"
  )
)
Karl Forner
  • 4,175
  • 25
  • 32