在特定名称空间中定义函数 [英] define a function in a specific namespace

查看:128
本文介绍了在特定名称空间中定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包"mypackage"(附加了一个命名空间"mypackage") 在此程序包中,有一个函数可以通过

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'

'mypackage::myfunction'

现在我想用另一个版本(已更新)替换我的功能.

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

我曾经做过

source(path)

其中path是定义了更新的"myfunction"的文件的路径

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

现在我移至R 2.14.x,该系统无法正常工作,因为显然R会首先检查同一名称空间中是否存在一个函数,如果存在,则会使用此函数而不是其他函数.

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?

推荐答案

请参见?assignInNamespace.例如

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

将对象foo分配给命名空间"mypackage"中名为"myfunction"的对象. foo可以是任何想要的对象,甚至是myfunction,但是如果在全局环境/工作区中也有myfunction,则需要小心确保调用mypackage::myfunction.

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.

这篇关于在特定名称空间中定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆