Changing the load order of files in an R package Changing the load order of files in an R package r r

Changing the load order of files in an R package


The Collate: field of the DESCRIPTION file allows you to change the order files are loaded when the package is built.

I stumbled across the answer to this question yesterday while reading up on Roxygen. If you've been documenting your functions with Roxygen, it can try to intelligently order your R source files in the Collate: field (based on where S4 class and method definitions are). This can be done by adding "collate" to the roclets argument of roxygenize. Alternatively if you're developing in RStudio there is a simple box that can be checked under Build->Configure Build Tools->Configure... (Button next to "Generate documentation with Roxygen").


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

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

#' @include class-a.rsetClass("B", contains = "A")

If any @include tags are present in the package, roxygen2 will set the Collate field in the DESCRIPTION.

You need to run generation of roxygen2 documentation in order to changes to take effect.