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?
Asked
Active
Viewed 3,989 times
26

pat shipan
- 715
- 6
- 13
-
You could "borrow" it. `borrowedfun <- pkg:::fun()` and put the package in Suggests. Making sure the original author is okay with it is a good idea. Or just ask them if you can just use the exact function in your own package. – Rich Scriven Sep 12 '15 at 06:33
-
1You can of course ask them or just include the function based on the license of the other package. – Roland Sep 12 '15 at 10:09
-
If it's only a single function from the package, why not just copy it? Of course, check the licence, make a note in the functions documentation and perhaps even email the author. As a comparison, I assume you have code you found on SO in your package? – csgillespie Sep 12 '15 at 14:54
-
4IIRC the use of `:::` is *not* allowed in CRAN packages. You should ask the original package author to export it, or, alternatively, permission to include a copy in your own package. – baptiste Sep 12 '15 at 20:33
2 Answers
35
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.
-
1I just tested this and it worked perfectly. Eliminates need to copy/paste function from other package. – pauljohn32 May 14 '18 at 06:16
-
1
16
Summarising comments from @baptise, and etc...:
:::
not allowed on CRAN, so options:- ask author to export it so you can use it in your package via standard imports or suggests.
- copy / lift a version of it and clearly cite within your package.

pat shipan
- 715
- 6
- 13