handling namespace with .onLoad()

I am using .onLoad() to set some variables that will be used by multiple functions in my package as global options. This seems like the appropriate way to handle that, but please correct me if I am wrong. Maybe I should be using internal functions instead?

The variables I am setting are using external packages. I am doing something like: mypkg.something = pkg::fun(x). This works for me and is compatible with devtools::test().

However, because NAMESPACE is generated automatically by Roxygen2, the packages I am using in .onLoad() do not get added there. This causes various errors with devtools::check().

What is the appropriate way to handle this?

I think if you define variables in your R files directly, they will be available to your package's functions. If you need a specific order from your file to be loaded you could use the @include roxygen tag to change the collate order. @include is used to say that one file needs another to work. Like a global.R loaded before any other.

The example is a bit complicated but ggplot2 is the first I thought of. Not simple because they decided to put global variables in environment (and not just in the ggplot2 package environment). You can do without this.
What is interesting:

  1. You can put your global variables in a R file like gggplot-global.R
  2. If you need to load this file first, use @include tag like in (aaa-.R](https://github.com/tidyverse/ggplot2/blob/master/R/aaa-.r), the first file that will be loaded. It will then load the global R file before any other. See ?update_collate help page.
  3. This roxygen tag will modify the collate order in Description.

See Writing R extension

A ‘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 in preference to ‘Collate’.

The R subdirectory contains R code files, only. The code files to be installed must start with an ASCII (lower or upper case) letter or digit and have one of the extensions13 .R, .S, .q, .r, or .s. We recommend using .R, as this extension seems to be not used by any other software. It should be possible to read in the files using source() , so R objects must be created by assignments. Note that there need be no connection between the name of the file and the R objects created by it. Ideally, the R code files should only directly assign R objects and definitely should not call functions with side effects such as require and options . If computations are required to create objects these can use code ‘earlier’ in the package (see the ‘Collate’ field) plus functions in the ‘Depends’ packages provided that the objects created do not depend on those packages except via namespace imports.

3 Likes