Making another package attach when my package is attached

I'm building a small package for local distribution among our analysts that encapsulates the connection code for our Oracle databases. I would like to make it so that attaching that package also makes available all of the functions from the package DBI.

In other words, I would like it so that instead of this

library(mypackage)
library(DBI)

one could do this and have exactly the same effect

library(mypackage)

I tried looking at what happens when you do library(tidyverse) and it attaches ggplot2, tibble, etc., and I see there is a do.call applying library to those packages, but if I just include library(DBI) in my package that doesn't do the trick. This seems like it should be something simple, but I don't know how to search for it.

Thanks!
Brian

1 Like

I may have found the right idea, doing @importFrom followed by @export, aka "re-exporting"?

E.g., as seen here in dplyr:

Would this be the preferred approach?

While re-exporting is "correct" in the sense that it will work I believe the easiest approach is including DBI in the Depends field of your DESCRIPTION file.

5 Likes

Ah-ha! "Depends" is the word I was looking for, thank you. Now that I know that, it becomes much easier to find it explained in places like here:
http://r-pkgs.had.co.nz/namespace.html#namespace
or here:
https://kbroman.org/pkg_primer/pages/depends.html

(Which it turns out I should be reading these anyway!)

Glad to be of service!

And as for the books I really recommend Hadley's R Packages. It was this book that got me started on the slippery slope of package development, all the way to CRAN submission.