I'd like to push a function inside a package namespace so it can access internal objects of that package (let's use stats package as an example). I've tried using
myfun <- function(x) print(x) env = loadNamespace("stats") assign("myfun", myfun , env)
But it is locked. So I've tried to unlock my object
unlockBinding("myfun", env)
Since myfun doesn't exist yet, I can't unlock it.
Any help ?
Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves.
A namespace gets created automatically when a module or package starts execution. Hence, create namespace in python, all you have to do is call a function/ object or import a module/package.
Namespace packages allow you to split the sub-packages and modules within a single package across multiple, separate distribution packages (referred to as distributions in this document to avoid ambiguity).
Along the line of @Hadley's solution, but using the environment of the namespace, how about:
environment(myfun) <- asNamespace('stats')
Why not just set the environment of your new function to the right place?
myfun <- function(x) print(x) environment(myfun) <- as.environment("package:stats")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With