Reputation: 127
Is it possible to include a "conditional import" in a package's DESCRIPTION file?
For example, I am developing a package which schedules system tasks. On Windows this is achieved with Task Scheduler and the taskscheduleR
package, on unix with the cronR
package. So intuitively it would be useful to do something like the following:
DESCRIPTION
Package: pkgname
Version: 0.0.1
[more fields]
Imports:
dplyr,
if (.Platform$OS.type == "windows") "taskscheduleR" else "cronR",
tidyr
I suppose it would be possible to write an .onAttach()
or similar which checks the system type and installs the relevant package if not already present, but that doesn't seem like a particularly nice solution - firstly, it relies on the user attaching the package while connected to the web before they can use it, and secondly it breaks the formal dependency chain.
My current approach is to include both packages in Suggests
, with the responsibility then on the user to install the correct package for their system.
Upvotes: 3
Views: 440
Reputation: 132706
I think this might be possible with a configure
shell script as described in Writing R Extensions. But I don't have experience doing that. You can also do platform-dependent stuff in your NAMESPACE files (that won't help here, but see the source of base package parallel for an example).
You could help your users by using that "The R and man subdirectories may contain OS-specific subdirectories named unix or windows." This allows you to have OS-specific code, which could then do the usual check for package availability.
E.g., in the windows subdirectory, you'd have something like:
if (requireNamespace("taskscheduleR", quietly = TRUE)) {
taskscheduleR::taskscheduler_create(...)
} else {
stop("Please install the taskscheduleR package to use this functionality")
}
Upvotes: 2