define a function in a specific namespace

2020-07-11 07:35发布

I have created a package 'mypackage' (with a namespace 'mypackage' attached) In this package there is a function that I can call either with

'myfunction'

or

'mypackage::myfunction'

Now I want to replace myfunction by another version (updated).

I used to do

source(path)

where path is the path of a file where the updated 'myfunction' is defined

Now I moved to R 2.14.x and this system doesnt work because apparently R checks first if there is a function inside the same namespace, and if there is one, it uses this one and not the others.

My question: how can I push the updated function to be in the same namespace as the package one?

标签: r namespaces
1条回答
够拽才男人
2楼-- · 2020-07-11 08:05

See ?assignInNamespace. For example

assignInNamespace("myfunction", foo, "mypackage")

will assign the object foo to the object named "myfunction" in namespace "mypackage". foo can be whatever object you want, even myfunction but you will need to be careful to ensure you call mypackage::myfunction if you also have myfunction in the global environment/workspace.

查看更多
登录 后发表回答