5

I'd like to write a R package. A small part of its functionality would be to save data into xlsx file. But this functionality would require a big and heavy dependency: library(xlsx). So I'd like to make this dependency somehow optional and lazy-loaded.

What is the Best Practice for it?

I guess I could simply library(xlsx) in the code of the function that need it, and handle possible failures of this command.

Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68
  • 1
    For usage inside functions, one could use `require`. – Roman Luštrik Aug 07 '13 at 10:42
  • If you only need some functions you can do `xlsx:::the_function_you_need(...)` – holzben Aug 07 '13 at 10:50
  • @holzbrn I know that, but AFAIK I'd first have to `require` that library. Loading this library takes several seconds, and I guess it takes a lot of resources (Java VM, and so on) – Adam Ryczkowski Aug 07 '13 at 11:03
  • I just tried it, you don´t need `require`, but I think the package must be installed, therefore this might be a problem if you forward your package to other users – holzben Aug 07 '13 at 11:20
  • @holzbrn I've just tried it myself too, and unfortunately the specific function I need, the `xlsx::read.xlsx`, doesn't work unless I `require(xlsx)`. Only then it loads its dependencies: `xlsxjars` and `rJava` and I guess do a lot of other initialization stuff as well. – Adam Ryczkowski Aug 07 '13 at 11:45
  • ah, ok nice to know... I tried it with `slam:::col_sums` – holzben Aug 07 '13 at 12:01
  • @AdamRyczkowski then that is a bug with xlsx, and you should complain to the author. – hadley Aug 07 '13 at 13:47

1 Answers1

-1

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

importFrom(xlsx, the_function_you_need)

along 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. There is some discussion of importFrom here: What is the benefit of import in a namespace in R?

Community
  • 1
  • 1
elzizi
  • 310
  • 1
  • 8
  • That is an interesting point. I believe most of the waiting time is due to the initialization of Java VM and friends, and I guess this is mandatory whatever small piece of the xlsx library I import. But I'll check that. – Adam Ryczkowski Aug 07 '13 at 11:39
  • Are you sure, the `importFrom` function is still supported? I can't find it in my R 3.0.1 – Adam Ryczkowski Aug 07 '13 at 11:51
  • `importFrom` is not a function. It is a line to be added to the `NAMESPACE` text file - which is part of any package you might write. The `NAMESPACE` file tells the package what "spaces" (e.g. other libraries) to look for what "names" (e.g. functions). But my understanding is somewhat limited - I just know it works! (check out: http://stackoverflow.com/questions/4371181/namespaces-in-r-packages) – elzizi Aug 07 '13 at 12:57
  • This will save time if the initialisation occurs when xlsx is attached to the search path (e.g. with library or require), but not if it occurs when the package is loaded (e.g. with a pacakge depedency) – hadley Aug 07 '13 at 13:46
  • @hadley Would you mind clarifying? Would `importFrom(PKG, FUN)` work if `Depends: PKG` were omitted from DESCRIPTION? And if so, would the function that called FUN have access to just the bits that it needs to run FUN (e.g. loading only the necessary dependencies within PKG)? – elzizi Aug 07 '13 at 13:57
  • @EliGurarie no, all packages that you import from in NAMESPACE must be also listed in the `Imports` in DESCRIPTION. Packages are the fundamental unit, not functions, so either a package is either loaded or not. – hadley Aug 07 '13 at 14:09
  • @hadley Got it, thanks. So no help in the NAMESPACE for the questioner. – elzizi Aug 07 '13 at 14:14