Using un-exported function from another R package?
Asked Answered
C

2

28

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?

Chromous answered 12/9, 2015 at 6:9 Comment(4)
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.Lucky
You can of course ask them or just include the function based on the license of the other package.Nephridium
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?Skean
IIRC 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.Bondman
C
18
  • 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.
Chromous answered 17/9, 2015 at 22:9 Comment(0)
I
38

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.

Imprecise answered 7/9, 2017 at 14:23 Comment(2)
I just tested this and it worked perfectly. Eliminates need to copy/paste function from other package.Calyptrogen
Is this practice still permitted on CRAN in 2022?Radack
C
18
  • 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.
Chromous answered 17/9, 2015 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.