Reputation: 169
I am trying to import a function from a non-CRAN repo into my package.
I know R searches CRAN for any package declared in the Imports:
field of the DESCRIPTION
file. Is there any way to, for example, import function
from package 'notoncran', which is only on Github (or some other non-CRAN place).
I've worked out a nondesirable workaround which would consist of bypassing the Imports:
field completely by defining my function as something like:
myfun <- function(a,b){
x <- require(notoncran)
if(!x){
print("installing notoncran because you don't have it...")
devtools::install_github('repo/withpackage')
require(notoncran)
}
...
}
I don't like this idea out of principle as you are installing a/several packages(s) to some extent without the user's consent, from a potentially unregulated (theoretically dangerous) source. This also reduces the readability of the function to some extent by weighing the function down with administrative business. Lastly, this method would eventually require running require()
or library()
, throwing all of the package's functions into the user's namespace which is never ideal.
Thanks for any help on this.
Upvotes: 7
Views: 1234
Reputation: 169
A super easy trick is to add a ‘remotes’ field into our DESCRIPTION
file specifying the username/package_name target of our target package on Github.
Remotes:
github::User/PackageNotOnCRAN
Import:
PackageNotOnCRAN
Suggests:
devtools,
testthat
Not only will this work very well for files on github (github::
), but works for git, bitbucket, local packages and more.
More information, how I figured it out.
Upvotes: 6