Change the general default setting in all package features

I would like to write a function that "resets" the default values ​​to a parameter common to all R-functions in the package.

Is it possible?

what I came up with is this, but it just returns a list of curry functions:

library(magrittr) function(parameter){ ls("package:packageName") %>% lapply(function(fxnName){ functional::Curry(get(fxn), paramOfInterest = parameter) } } 

Pipe %>% from magrittr package.

I think one of the caveats that he does is the loss of documentation.

Perhaps I could solve this by referring to the attribute being modified and stored in the object in the package?

+7
r
source share
1 answer

While this could be done by setting the global environment, in my opinion, more clear, since the default behavior can be easily restored using the detach environment:

First, some demo functions, since I don’t know which package you wanted to use:

 packageDemo <- new.env() makeFun <- function(x) {x;function(param) c(x, param)} for (i in paste0("f", letters)) assign(i, makeFun(i), packageDemo) 

Now here is my curry package function. A character vector requires either an environment or a package name (for example, "package:myPackage" ). All functions in the environment are displayed in the currency and are added to the new environment, which is returned. Then it could be attach ed.

 curryPackage <- function(package, ..., functionNames = NULL){ if (is.character(package) && !grepl("^package:", package)) { package <- paste0("package:", package) } fnNames <- ls(package) if (!is.null(functionNames)) { fnNames <- fnNames[fnNames %in% functionNames] } fnNames <- fnNames[vapply(fnNames, function(x) is.function(get(x, package)), logical(1))] fnList <- lapply(fnNames, function(x){ functional::Curry(get(x, package), ...) }) names(fnList) <- fnNames list2env(fnList) } 

Now some demos

 attach(packageDemo) fc("Testing") #[1] "fc" "Testing" fc() #Error in fc() : argument "param" is missing, with no default newPack <- curryPackage(packageDemo, param = "demo") attach(newPack) #The following objects are masked from packageDemo: # # fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn, fo, fp, fq, fr, fs, ft, fu, fv, fw, fx, fy, fz fc("Testing") #Error in (function (param) : unused argument ("Testing") fc() #[1] "fc" "demo" 
0
source share

All Articles