Lazy package dependency on R

I would like to write an R package. A small part of its functions would be to save data to an xlsx file. But this function will require a big and heavy dependency: library(xlsx) . Therefore, I would like to make this dependency somehow optional and lazy.

What is his best practice?

I think I could just library(xlsx) in the code of the function he needs and handle the possible crashes of this command.

+4
source share
1 answer

I believe the most reliable way to do this is to add the following line to the NAMESPACE your package:

 importFrom(xlsx, the_function_you_need) 

together with

 Depends: xlsx 

in the DESCRIPTION file. As far as I understand, this will give your package access to the function you want without loading the entire library. importFrom discussed importFrom : What is the advantage of importing in the namespace in R?

0
source

Source: https://habr.com/ru/post/1495715/


All Articles