I've been looking around at multiple sources trying to find out how to properly implement classes in R but I haven't found the answer I'm looking for. I learned to code in java so my understanding of object oriented program is through the use of a compiled language, in which I could create a class definition in a .java document and then import the class in another .java document in the same package, where I can then create objects and call the class methods. Is there a similar protocol for R? I'm trying to figure out how I can create a class object and utilize its methods in a different .R document than the one I defined the class in.
Basically, I am building a script to import a file into R and then do a lot of data manipulation to get my desired data frame. I have created a class that handles the data manipulations. This code is in the ImportClass.R file:
# Class definition for data importing
DataImport <- setRefClass("DataImport", fields = c("startDate"), where = Sys.getenv("dataImportClassEnv") # See the setEnvironment.R file for the value of dataImportClassEnv
# -----------------------------------------------------------------------------
# Example method
# -----------------------------------------------------------------------------
DataImport$methods(fileImport = function(FilePath)
{
Method code here
}
)
I also have a script in which my environment variables are set. Users of this code will have the data on their local machine but I don't want any references to local file paths in the class definition or the script that calls the class methods. My preference is to create a script that sets the file paths in a way that they can be utilized in my other scripts. This is the purpose of setEnvironment.R
# Sets environment variables
# Input below the location of the .R files on your local machine corresponding to this model. This variable is used to specify the 'location' attribute for the DataImport class.
Sys.setenv(dataImportClassEnv = "/Users/Nel/Documents/Project/Working Docs")
Finally, I have a script that I want to use to actually build my data frame. This script needs to create an object from the DataImport class, so I need to source() the ImportClass.R file. ImportClass.R also relies on the setEnvironment.R file to run, so it also must be sourced. Here is the outline of my script, dataScript.R:
library(XML)
source("setEnvironment.R", local = TRUE)
source("ImportClass.R", local = TRUE)
# Create instance of DataImport class
importer <- DataImport$new(startDate = "2012-01-01")
# Go on to call methods on 'importer'
The trouble is I get the following error when calling the source function here.
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'setEnvironment.R': No such file or directory
All of these files are saved in the same place, "/Users/Nel/Documents/Project/Working Docs", and I don't want that file path in my dataScript.R code. Is there a way to get this working?