Changing file upload order in package R

I am writing a package for R in which the exported functions are decorated with a higher order function that adds error checking and some other boilerplate code.

However, since this code is at the top level, it is evaluated after parsing. This means that you need to download package files.

To give an equivalent but simplified example, suppose I have a package with two files (Negate2 and Utils), and I require Negate2.R to be loaded first for the isfalse () function, which should be determined without throwing an error.

# /Negate2.R Negate2 <- Negate # ------------------- # /Utils.R istrue <- isTRUE isfalse <- Negate2(istrue) 

Is it possible to structure NAMESPACE, DESCRIPTION (collate) or another package file to change the file upload order? The inner workings of the R and CRAN package structure are still black magic for me.

You can work around this problem using inconvenient hacks, but the least repetitive way to solve this problem. The wrapper function must be a higher-order function, since it also changes the semantics of the function call of the input function. The code package is heavy (~ 6000 lines, 100 functions), so repetition will be ... problematic.

Decision

As @Manetheran points out, to change the order of loading, you simply reorder the file names in the DESCRIPTION file.

 # /DESCRIPTION Collate: 'Negate2.R' 'Utils.R' 
+7
r build package
source share
2 answers

The Collate: field of the DESCRIPTION file allows you to modify the order files that are loaded when the package is created.

I came across an answer to this question yesterday while reading on Roxygen . If you document your functions with Roxygen, it might try to reasonably arrange the R source files in the Collate: field (depending on where the class and S4 method definitions are indicated). This can be done by adding "collate" to the roclets roxygenize argument. Alternatively, if you are working in RStudio , there is a simple block that can be checked under Build-> Configure Build Tools-> Configure ... (Button next to "Creating Documentation with Roxygen").

+6
source share

R downloads files in alphabetical order. To change the order, the Collate field can be used from the DESCRIPTION file.

roxygen2 provides an explicit way of saying that one file must be loaded before the other: @include. The @include tag provides a list of space-separated file names that must be loaded before the current file:

 #' @include class-ar setClass("B", contains = "A") 

If the package contains @include tags, roxygen2 will set the Collate field to DESCRIPTION.

You need to start the roxygen2 documentation generation for the changes to take effect.

+1
source share

All Articles