2

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?

tjnel
  • 643
  • 1
  • 9
  • 19

1 Answers1

0

This is a minimal example, using the simplest S3 type of class, perhaps enough to get started:

Begin by creating file1.R and sourceing it:

writeLines('
setClass("myClass")
f1 <- function(x) cat(rep(x,2))
setMethod("print", signature="myClass", definition = f1)
' 
,con="file1.R") 
### note use of alternating apostrophes ' then "
source("file1.R")

a2 <- c(4,5,6)
class(a2) <- "myClass"
print(a2)

gives:

4 5 6 4 5 6>

Note that to use print in setMethod, isGeneric("print") == TRUE.

For custom methods see ?setGeneric

dardisco
  • 5,086
  • 2
  • 39
  • 54
  • Thanks for the reply. I edited my original post to add details because I ran into more issues when using the source() function. – tjnel Apr 05 '13 at 06:25
  • That's a bit trickier. If I understand, the essential difficulty is `source`ing a file without having the reference to the file location (path) appear in the code. Perhaps you could clarify why this is necessary to help other answerers. Could you host the files remotely then refer to that location, for example in a minimal package on [github](https://github.com/), then refer to this using `install_git()` from [`devtools`](http://cran.r-project.org/web/packages/devtools/devtools.pdf) before running the rest of the code? – dardisco Apr 05 '13 at 17:45
  • Yes, you answered my first question perfectly but in using the source() function I realized it required including file paths in my main script. I want the file paths in a separate script to keep code clean (this is a request from the end-users). If you notice in my setEnvironment.R code, I thought I could use Sys.setenv() to assign file paths to environment variables. I thought each time R opened to this environment those variables would be available to dataScript.R without having to source, but this doesn't work as I expected. I'll look into your suggestion of hosting remotely. Thanks. – tjnel Apr 06 '13 at 18:02
  • Here's [an example](http://stackoverflow.com/questions/9002544/how-to-add-functions-in-an-existing-environment/9013242#9013242) assigning a function in an environment which you may be able to adapt to meet your needs and may be simpler than package creation. – dardisco Apr 06 '13 at 18:34