简体   繁体   English

当在另一个包中定义对象时,如何在R中调用新函数?

[英]How do I call the new function in R when the object is defined in another package?

Suppose I'm creating a package and I'd like to define a function that creates an object where the object is defined in another package. 假设我正在创建一个包,我想定义一个函数来创建一个对象,在该对象中,对象在另一个包中定义。

For example: get_empty_mtx <- function() return(new("dgCMatrix")) 例如: get_empty_mtx <- function() return(new("dgCMatrix"))

If I type library(Matrix) , this will work, but when I'm making my own package, I like to use :: when referencing things from other packages. 如果我输入library(Matrix) ,这将有效,但是当我创建自己的包时,我喜欢在引用其他包时使用:: I can't do Matrix::new("dgCMatrix") as new is not a function from the Matrix package. 我不能做Matrix::new("dgCMatrix")因为new不是Matrix包中的函数。

You can use the getClassDef function to get a class definition from a specific package and then call new() on that. 您可以使用getClassDef函数从特定包中获取类定义,然后在其上调用new() For example 例如

new(getClassDef("dgCMatrix", getNamespace("Matrix")))

and new(getClassDef("dgCMatrix", "Matrix")) also seems to work despite the documtation saying where should be an environment. new(getClassDef("dgCMatrix", "Matrix"))似乎也尽管documtation说工作的where应该是一个环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R包中插入新功能? - How do I insert a new function into my R package? 如何清除错误:对象 <some function> 不是&#39;名称空间导出: <some package> &#39;在构建我的R包时出现了什么? - How do I clear Error: object <some function> is not exported by 'namespace:<some package>' which shows up when building my R package? 如何在 R 中调用“呼叫” object - How do I call a 'call' object in R 我如何要求R从用户定义的程序包运行特定功能以在每次加载该程序包时运行? - How do I ask R to run a specific function from a user defined package to run everytime I load that package? R:如何让我的包使用另一个包? - R: How do I make my package use another package? 如何在 R function 中创建一个新的 object,我可以在 ZC1C425Z068E687A9AB4F14D1 中使用它 - How do I create a new object in an R function, that I can use within the function 如何使用其他名称安装R软件包? - How do I install an R package under another name? 在R中运行spark包不起作用,如何将一个spark包称为R? - Running spark package in R isn't working, how do I call a spark package into R? 如何在 r 中编写用户定义的函数? - How do I write a user defined function in r? 如何使用R中的reshape函数引用已定义的对象? - How do I refer to defined objects using the reshape function in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM