簡體   English   中英

當在另一個包中定義對象時,如何在R中調用新函數?

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

假設我正在創建一個包,我想定義一個函數來創建一個對象,在該對象中,對象在另一個包中定義。

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

如果我輸入library(Matrix) ,這將有效,但是當我創建自己的包時,我喜歡在引用其他包時使用:: 我不能做Matrix::new("dgCMatrix")因為new不是Matrix包中的函數。

您可以使用getClassDef函數從特定包中獲取類定義,然后在其上調用new() 例如

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

new(getClassDef("dgCMatrix", "Matrix"))似乎也盡管documtation說工作的where應該是一個環境。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM