Subdirectory in package R

I am creating an R package and would like to organize an R subdirectory with subdirectories. Since only the function defined in the R files in the root directory is exported, I added this code to a single file in the root directory:

sourceDir <- function(path, trace = TRUE, ...) { for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) { print(nm) if(trace) cat(nm,":") source(file.path(path, nm), ...) if(trace) cat("\n") } } sourceDir("R/DataGenerator") 

When I use "CRTL + SHIFT + B" on RStudio, I see that nm files are received. But once the package is loaded, none of the functions defined in the R / DataGenerator subdirectory are available without using :: or using :.

How can I export functions defined in R subdirectories? Is it possible?

+2
source share
2 answers

Use the Collect field in the DESCRIPTION file to specify the paths to the files to be included.

 Collate: foo.R bar/baz.R 

The helper to create a sort string might be something like

 fls = paste(dir(pattern="R", recursive=TRUE), collapse=" ") cat(strwrap(sprintf("Collate: %s", fls), exdent=4), sep="\n") 
+1
source

As pointed out in the discussion in the comments on the accepted answer between Martin Morgan and me, this does not seem to work in current versions of R. My way around the slightly better file organization is to prefix the files using what would be the names of the subdirectories.

0
source

Source: https://habr.com/ru/post/1265016/


All Articles