rr-packagetidytable

R package: global import of equivalent package if preferred one fails


This question is inspired by the specific nature of tidytable as an intentional drop-in replacement for entire tidyverse packages and my own circumstances.

I'm developing a couple of R packages at work that rely extensively on tidytable. The R environment is quite limited and centrally controlled (and that's impossible to change) and it would be advantageous for the packages to automatically fall back on dplyr + tidyr + purrr. These are more likely to be centrally added/updated to the correct environment whether I'm around bugging the right people or not.

I'm currently using @import tidytable, as I repeatedly use a long list of its functions. Is it possible to get my package to fall back on dplyr, tidyr and purrr if tidytable fails to load? My package runs the same whether I import one or the others; but I'd prefer tidytable when available as it is noticeably faster.

I'm aware of certain workarounds for one or two functions but not across the whole package, probably because the tidytable/dplyr+tidyr+purrr match not only of functionality but also syntax is rather unusual.


Solution

  • Some assumptions:

    Since the packages you need will not be in Imports:, then when somebody does library(yourpackage), R will not automatically import the either or the other package(s). In this case, you have two options, depending on your comfort with using the .onLoad or .onAttach functions (see ?ns-hooks):

    Notes:

    1. If you aren't familiar with the difference between library and require (and their related functions), see https://stackoverflow.com/a/51263513/3358272, https://yihui.org/en/2014/07/library-vs-require/, https://r-pkgs.org/namespace.html#search-path.