I am writing a class definition that will be extremely long, and I'd like to source the code for the methods from separate files. Most of the time, that seems to work, but I have encountered something strange when trying to return a data.frame. When the code is written directly in the class definition, the data.frame returns fine. When it is sourced, what gets returned is a list of length 2, where the first element (named value
) contains the data.frame, and the second element (named visible
) contains an atomic logical TRUE
. How can I get the method to return just the data.frame while leaving the code for the method in a separate file?
Here is the main file:
#MainScript.R
library(R6)
MyClass = R6Class(
classname = "myclass",
private = list(Frame = NA),
public = list(
initialize = function(){},
setFrame = function(x){private$Frame = x},
getFrame = function(){
MyData = private$Frame
return(MyData)
},
getFrame2 = function(){source("getFrame2.R", local = T)}
)
)
data1 = data.frame(v1 = rnorm(5,1,2), v2 = rnorm(5,1,2))
current = MyClass$new()
current$setFrame(data1)
current$getFrame()
current$getFrame2()
Here is the method source file:
#getFrame2.R
MyData = private$Frame
return(MyData)
Note that the code for the two methods is exactly the same. The code can be downloaded from github.