I am using a function factory defined by someone else and I cannot change it. It is common to need to generate several functions with this factory at the beginning of every run. In my attempt at a toy example, it's as if I need many such power_
functions frequently. Currently as a user of otherpackage
, I must include lines like power2 <- power_factory(2)
in my scripts.
otherpackage::power_factory <- function(exp) {
function(x) {
x ^ exp
}
}
power2 <- power_factory(2)
power3 <- power_factory(3)
I would like to avoid these lines by writing my own package, so I can instead use mypackage::power2()
in my scripts. Normally, if I wanted to wrap these functions in a package, I could just use the factory to build them inside the package. However, this factory depends on some outside input, here credential
, that I cannot write inside my package. This makes the factory error if I put power2 <- power_factory(2)
inside the package:
# credential <- "my_secret"
otherpackage::power_factory <- function(exp) {
print(credential)
function(x) {
x ^ exp
}
}
power2 <- power_factory(2)
#> Error in print(credential): object 'credential' not found
Created on 2019-03-07 by the reprex package (v0.2.1)
Is there a way around this problem? The end goal, as above, is to be able to call mypackage::power2()
instead of needing to do power2 <- power_factory(2)
and many variations on such at the start of each script.