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

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 4 16:19:10 CEST 2017


On 04/08/2017 5:51 AM, frédéric Clement wrote:
> 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"))
> }

Usually you should use .onLoad, not .onAttach.  If someone who hasn't 
attached your package calls one of the functions using dbName::fn(...),
your package will be loaded but not attached.  Similarly if another 
package imports one of your functions.


>
>
> ## 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")
> }

However, for this particular use, it might be bad to put the code even 
in .onLoad.  A better way to go would be to have a function in your 
package that calls dbName_create_pool the first time you call it, and 
just returns the same value on subsequent calls.  For example,

getcon <- local({
   con <- NULL
   function() {
     if (is.null(con))
       con <<- dbName_create_pool()
     con
   }})


This way nobody is prompted for a password unless they actually need to 
access the database.

>
> #' 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
> }

Not sure why you'd want these 13 lines instead of editing your NAMESPACE 
file, but that's your choice...

Duncan Murdoch

>
> --- 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]]
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>



More information about the R-package-devel mailing list