eval - Patch base::library with wrapper in R -
eval - Patch base::library with wrapper in R -
inside r package, i'm trying patch base::library()
function in r set position of loaded packages in search path. haveve defined several environments (all named env:<something>
) , want create sure libraries placed below these environments in search path.
# wrap around library function. library = function(..., pos = null) { print("new library function!") if (is.null(pos)) { pos <- grep("env:", search()) pos <- if (length(pos) == 0) 2 else max(pos) + 1 } base::library(..., pos=pos) }
when assign function in console, runs fine:
> library(stats) [1] "new library function!" > eval(parse(text = "library(stats)")) [1] "new library function!" > eval(parse(text = "library(stats)"), envir = globalenv()) [1] "new library function!"
when define above wrapper function within package, build , load in new r session, next executes expected:
> library(mypackage) > mypackage:::library(stats) [1] "new library function!"
but, when using eval()
envir
argument within function in mypackage
, new definition of library()
not retrieved:
# functions defined in mypackage testlibrary1 = function(...) { library(...) } testlibrary2 = function(code) { eval(parse(text = code)) } testlibrary3 = function(code) { eval(parse(text = code), envir = globalenv()) }
in console, next results:
> mypackage:::testlibrary1(stats) [1] "new library function!" > mypackage:::testlibrary2("library(stats)") [1] "new library function!" > mypackage:::testlibrary3("library(stats)") >
the lastly function, testlibrary3()
, did not utilize new wrapper function.
i want functions phone call library()
within mypackage
utilize wrapper function. can help me out?
i guess problem following, question did not include reproducible illustration (i.e., uploading bundle somewhere) hard tell.
as long library
function not exported bundle via namespace
not visible. consequently, available library
function eval base::library()
.
note while function resides in namespace of bundle calling environment mypackage:::testlibraryx()
still global environment. there library
functions not available. seek export , see if helps.
r eval wrapper overrides install.packages
Comments
Post a Comment