Including R Scripts in R Packages

I want to share some software as a package, but some of my scripts do not seem to be very natural functions. For example, consider the following code fragment, where "raw.df" is a data frame containing variables, both discrete and continuous. The functions "count.unique" and "squash" will be defined in the package. The script divides the data frame into two frames, cat.df, which are considered categorical data, and cts.df, as continuous data.

My idea of ​​how this will be used is that the user will read in the data frame "raw.df", the source of the script, and then interactively edit "cat.df" and "cts.df", possibly combining some categories and conversion of some variables.

dcutoff <- 9 tail(raw.df) (nvals <- apply(raw.df, 2, count.unique)) p <- dim(raw.df)[2] (catvar <- (1:p)[nvals <= dcutoff]) p.cat <- length(catvar) (ctsvar <- (1:p)[nvals > dcutoff]) p.cts <- length(ctsvar) cat.df <- raw.df[ ,catvar] for (i in 1:p.cat) cat.df[ ,i] <- squash(cat.df[ ,i]) head(cat.df) for(i in 1:p.cat) { cat(as.vector(table(cat.df[ ,i])), "\n") } cts.df <- raw.df[ ,ctsvar] for(i in 1:p.cts) { cat( quantile(cts.df[ ,i], probs = seq(0, 1, 0.1)), "\n") } 

Now this, of course, could be turned into a function that returns a list containing nvals, p, p.cat, cat.df, etc .; however for me it seems pretty ugly. However, the only condition for including scripts in the package is the "demo" folder, which seems to be inappropriate for the correct path. Consultations on how to proceed will be gratefully received.

(But gratitude would not be formally expressed, as it seems that the use of commentary to express gratitude is outdated.)

+5
source share
1 answer

It is better to encapsulate your code in a function. Returning a list incorrectly, for example S3 objects is just a list with an attribute class .

 object <- list(attribute.name = something, ..) class(object) <- "cname" return (object) 

You can also use the inst folder (as indicated in the Dirk comment), since the contents of the inst subdirectory will be copied recursively to the installation directory.

  • you create inst folder:

     inst ----scripts some_scripts.R 
  • You can call it from a function in your package and use the system.file mechanism to load it.

     load_myscript <- function(){ source(system.file(package='your_pkg_name','scripts/some_scripts.R')) } 
  • You call it like any other function in your package:

     load_myscript() 
+4
source

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


All Articles