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?
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.
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.
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.
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.
Summarising comments from @baptise, and etc...:
:::
not allowed on CRAN, so options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With