Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using un-exported function from another R package?

Tags:

r

cran

I often use utility type functions from other packages that are un-exported: pkg:::fun(). I am wondering if I can use such a function within new functionality/scope in my own R package. What is the correct approach here? Is including the package in my description file enough?

like image 493
pat shipan Avatar asked Sep 12 '15 06:09

pat shipan


People also ask

What does @export mean in R?

The @export docstring informs Roxygen to to put the function name in the package NAMESPACE file which means it can be accessed by users after they run library(<packagename>) . It should be used for functions that you want consumers of your package to be able to use directly.

What is a namespace in R?

Namespaces are the environment where all the functions of a package live. The parent environments of namespaces are the imports environments, which contain all the functions imported from other packages.

How do you make an AR package?

We can build a source package (i.e., a zipped version of the R package) in Rstudio by selecting Build > Build Source Package . This will create a zipped package outside of the package directory, which would be what we would need to build if we wanted to submit our package to CRAN.


2 Answers

Another trick is using getFromNamespace():

fun <- utils::getFromNamespace("fun", "pkg")

The only advantage over ::: is that you don't get any NOTEs and it's allowed on CRAN. Of course, this is not good practice as a hidden change in pkg can break your package.

Note: With roxygen2 you have to add the utils package to the Imports field of your DESCRIPTION file to fulfill CRAN's requirements. Alternatively, you can put it in your NAMESPACE manually.

like image 148
jakob-r Avatar answered Sep 18 '22 05:09

jakob-r


  • Summarising comments from @baptise, and etc...:

  • ::: not allowed on CRAN, so options:

    1. ask author to export it so you can use it in your package via standard imports or suggests.
    2. copy / lift a version of it and clearly cite within your package.
like image 32
pat shipan Avatar answered Sep 21 '22 05:09

pat shipan