Search code examples
rr-packageroxygen2

R package: sourcing scripts from folder on package load


My idea is simple, I have a wrapper package that is ready to load and execute R and Python scripts (through reticulate).

Despite the requirement for a common file/code structure, the idea is that if a user wants to define a new method for the workflow (say, updated taxonomy), it would be enough to add a new script in the scripts folder (i.e., place files in rules/, py/..., and rebuild).

What is the proper way to make this work?

I've tried loading the files through .onLoad, i.e.,

.onLoad <- function(lib, pkg){
  require(reticulate) #I know require is bad practice; this is for example purposes
  files <- dir('rules')
  lapply(files, function(w){
     source(sprintf('rules/%s', w))
     source_python(sprintf("py/%s/rule.py", w))
  })
}

But this seems to be looking at local directories, rather than inside the package.

I have found that R is able to load .rda or .RData files from data/ directory, or .txt, .csv, but that's about it. What about extensions, like sourcing files as in my example?

Any help would be appreciated!


Solution

  • The folder inst/exdata allows for adding any kind of data within an R package. This might be the right place to store .R or .py files not directly related to the core code of your package. You can access these files with system.file("extdata", "file.ext", package="pkg"). See also extdata in R packages.