Extension of my comment, consider changing it to
cxn <- NULL
#' @param name character, name of the table
#' @param cxn database (DBI) connection object
#' @import DBI
#' @import RPostgres
#' @import dplyr
#' @export
db <- function(name, cxn = cxn) {
if (missing(cxn) && is.null(cxn)) {
cxn <<- DBI::dbConnect(RPostgres::Postgres(), service = 'plasticemission')
}
dplyr::tbl(cxn, name)
}
This way, you do not attempt connection instantiation when the package is loaded, just when the function is called. I provided an override-able variable cxn
in the call in case you ever have need for multiple, temporary, or non-standard connections.
If you do really want it to be connected as soon as you load the package, consider using .onLoad
(or .onAttach
), with something like:
# it might be better defined here, not in your other function, but once is enough
cxn <- NULL
.onLoad <- function(libname, pkgname) {
if (getOption("mypackage.autocxn", default = FALSE)) {
if (is.null(cxn)) {
cxn <<- DBI::dbConnect(RPostgres::Postgres(), service = 'plasticemission')
}
}
}
In this case, I made it an explicit requirement for auto-connect to set a global option "mypackage.autocxn"
(in this case as a logical). The name of the option is completely arbitrary, and you can do all sorts of flexible things here such as
.onLoad <- function(libname, pkgname) {
if (length(opts <- getOption("mypackage.autocxn", default = NULL)) &&
is.null(cxn)) {
cxn <<- do.call(DBI::dbConnect, c(list(drv = RPostgres::Postgres()), args))
}
}
And somewhere in your personal setup (perhaps your ~/.Rprofile
), you can set
```lang-r
options(mypackage.autocxn = list(service = 'plasticemission'))
```