Implementing a Class in a new Script in R -
i've been looking around @ multiple sources trying find out how implement classes in r haven't found answer i'm looking for. learned code in java understanding of object oriented program through use of compiled language, in create class definition in .java document , import class in .java document in same package, can create objects , call class methods. there similar protocol r? i'm trying figure out how can create class object , utilize methods in different .r document 1 defined class in.
basically, building script import file r , lot of data manipulation desired data frame. have created class handles data manipulations. code in importclass.r file:
# class definition data importing dataimport <- setrefclass("dataimport", fields = c("startdate"), = sys.getenv("dataimportclassenv") # see setenvironment.r file value of dataimportclassenv # ----------------------------------------------------------------------------- # example method # ----------------------------------------------------------------------------- dataimport$methods(fileimport = function(filepath) { method code here } )
i have script in environment variables set. users of code have data on local machine don't want references local file paths in class definition or script calls class methods. preference create script sets file paths in way can utilized in other scripts. purpose of setenvironment.r
# sets environment variables # input below location of .r files on local machine corresponding model. variable used specify 'location' attribute dataimport class. sys.setenv(dataimportclassenv = "/users/nel/documents/project/working docs")
finally, have script want use build data frame. script needs create object dataimport class, need source() importclass.r file. importclass.r relies on setenvironment.r file run, must sourced. here outline of 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 call methods on 'importer'
the trouble following error when calling source function here.
error in file(filename, "r", encoding = encoding) : cannot open 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 saved in same place, "/users/nel/documents/project/working docs", , don't want file path in datascript.r code. there way working?
this minimal example, using simplest s3
type of class
, perhaps enough started:
begin creating file1.r
, source
ing 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 ' " source("file1.r") a2 <- c(4,5,6) class(a2) <- "myclass" print(a2)
gives:
4 5 6 4 5 6>
note use print
in setmethod
, isgeneric("print") == true
.
for custom methods see ?setgeneric
Comments
Post a Comment