Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking multiple files while creating a package in R

I am trying to create a package in R wherein I have created lots of new custom Classes. Each class is in a different file. The Classes inherit from a parent class and inherit to other classes.

While running my codes I call each of them like this

source("package/father.R")
source("package/son.R")
source("package/grandson.R")     

Definition for Some of the methods needed by the grandson Class in defined in Son class. I use package.skeleton() to call each of them and create a package and it seems to work fine. But when running R CMD Check(and when trying install into R), it throws an error because the function tries to call the files in the alphabetical order and so the file grandson.R is called before son.R and it shows and error saying that the methods has not been defined. If I change the names to zgrandson.R, R called that file the last, and everything seems to work fine, but this is evidently not a solution for the problem.

I have read tutorials for creating packages, but all of them seem to deal with simple cases where there is no inheritance/calling other files in R. Hope I have made myself clear.

like image 550
Alex Joseph Avatar asked Sep 07 '12 05:09

Alex Joseph


People also ask

How do I put multiple files in different names in R?

If you are creating multiple datasets in R and wish to write them out under different names, you can do so by looping through your data and using the gsub command to generate enumerated filenames.

How do I create a new package in R?

To get started on a proper R package complete with documentation, the best thing to do is to create a new R project. To do this in Rstudio, go to File > New Project... ; the box below should pop up. Note that we could have started with a project right away, creating a new folder with the New Directory option.


1 Answers

As far as I understand, you can use the Collate field in the DESCRIPTION file to control this.

Quoting from the Writing R Extensions manual:

An ‘Collate’ field can be used for controlling the collation order for the R code files in a package when these are processed for package installation. The default is to collate according to the ‘C’ locale. If present, the collate specification must list all R code files in the package (taking possible OS-specific subdirectories into account, see Package subdirectories) as a whitespace separated list of file paths relative to the R subdirectory. Paths containing white space or quotes need to be quoted. An OS-specific collation field (‘Collate.unix’ or ‘Collate.windows’) will be used instead of ‘Collate’.

So, you could specify:

Collate:
  father.r
  son.R
  grandson.r

Or simply rename the files in such a way that lexicographic sorting order will result in the correct collation order, as you indicated in your question.


But also see this answer by @DirkEddelbuettel on a similar question.

like image 154
Andrie Avatar answered Oct 16 '22 19:10

Andrie