Linking multiple files when creating a package in R

I am trying to create a package in R where I created many new custom classes. Each class is in a different file. Classes inherit from the parent class and inherit other classes.

During the launch of my codes, I call them each of them

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

Defining some methods needed for a grandson class defined in the Sleep class. I use package.skeleton () to call each one of them and create a package, and it seems to work fine. But when R CMD Check starts (and when trying to install it in R), it gives an error, because the function tries to call the files in alphabetical order, and therefore the grandson.R file is called before son.R, and it shows, and the error says that the methods not defined. If I change the names to zgrandson.R, R named this file last, and everything seems to work fine, but this obviously is not the solution.

I read tutorials for creating packages, but they all seem to be dealing with simple cases where inheriting / finding other files in R. I hope I made it clear.

+8
inheritance class r packages
source share
1 answer

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

Quote from the R Extension Writing Guide :

An 'The Collate field can be used to control the sort order for R code files in a package when they are processed for a montage package. The default value is sorting according to the locale "C". If present, the sort specification should contain a list of all R code files in the package (taking into account possible OS subdirectories, see Package of subdirectories) as a list of files separated by spaces in the paths relative to the R subdirectory. Paths containing spaces or quotes should be indicated. An OS-specific sorting field ('Collate.unix or' Collate.windows) will be used instead of 'Parse.

So you can specify:

 Collate: father.r son.R grandson.r 

Or simply rename the files so that the lexicographic sort order results in the correct sort order, as you indicated in your question.


But also see this answer from @DirkEddelbuettel for a similar question.

+5
source share

All Articles