Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add objects to package namespace

Tags:

r

r-package

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 ?

like image 561
Etienne Racine Avatar asked Jun 22 '10 14:06

Etienne Racine


People also ask

What is namespace object Python?

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.

How do you create a namespace in Python?

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.

What is a package namespace?

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).


2 Answers

Along the line of @Hadley's solution, but using the environment of the namespace, how about:

environment(myfun) <- asNamespace('stats') 
like image 146
Mark Bravington Avatar answered Sep 24 '22 18:09

Mark Bravington


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") 
like image 22
hadley Avatar answered Sep 20 '22 18:09

hadley