[R-pkg-devel] DBI connection at package load

frédéric Clement f.clems at gmail.com
Fri Aug 4 11:51:01 CEST 2017


Hello,

I want to use a package to load and set up a working environment (Only
intern used). It has function, but also create a connection object
(RPostgreSQL) to make dplyr::tbl implicitely use this connection.

After trying to save the connection object in "Data\"... and failed. I made
the following work around.

Is it the right way to do it, or should it be solved in other ways.

PS: This is my first attent at package development experience, I mainly
follow http://r-pkgs.had.co.nz/

regards

frederic Clement

----- the script----

.onAttach <- function(libname = find.package("dbName"), pkgname = "dbName")
{
  packageStartupMessage("Intranet Company required")

  dbNameConPool = dbName_create_pool()
  assign("dbNameConPool", dbNameConPool, envir =
as.environment("package:dbName"))
}


## This is a function so that I can call it in .onAttach
##
dbName_create_pool = function(){

    DBI::dbConnect(drv = RPostgreSQL::PostgreSQL(),
                 user = "dbName",
                 password = .rs.askForPassword("Enter password:"),
                 port = 0000,
                 host = "dbName-int.company.com")
}


#' Dummy function
#'
#' Only used for package build purpose
#'     to import the usefull function.
#' @return NULL
#' @export
#' @import dplyr dbplyr DBI magrittr RPostgreSQL tidyr
#'
#' @examples
#' fun_import()
fun_import <- function(){
  NULL
}

--- session

R version 3.4.1 (2017-06-30)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252
LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
LC_TIME=German_Germany.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list