How to call a new function in R when an object is defined in another package?

Suppose I create a package, and I would like to define a function that creates an object in which the object is defined in another package.

For instance: get_empty_mtx <- function() return(new("dgCMatrix"))

If I type library(Matrix), it will work, but when I create my own package, I like to use ::when referring to things from other packages. I can not do Matrix::new("dgCMatrix")it because the new one is not a function from the Matrix package.

+4
source share
1 answer

You can use the function getClassDefto get the class definition from a specific package, and then call it new(). for instance

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

new(getClassDef("dgCMatrix", "Matrix")) , , , , documtation , where .

+2

Source: https://habr.com/ru/post/1682329/


All Articles