Hi all, I am developing an R package (lets call it dbr) at work so users can easily connect via ODBC to a database without having to remember any boilerplate. The workflow would be to assign a variable to dbr::connect_database.
The Issue:
While all it does is wrap/abstract away a standard odbc call, it does not pass the "Connections Contract" that produces the Connections pane information in RStudio. Given that odbc already has this code, I'm guessing I am missing some quirk of R that is preventing this from being passed.
What I Found:
I did some research on this and found information about the Connections Contract. Given that odbc already has this built in, and I am simply wrapping the function, it seems that I shouldn't have to rewrite this from scratch.
https://db.rstudio.com/advanced/contract/
My Code
This code is reproducible if you have set up an ODBC connection. When connecting to DB via the wrapper function, everything works except the Connections Pane. When calling the exact code outside of the function, the Connections Pane reappears.
connect_database <- function(dsn = "database") {
DBI::dbConnect(odbc::odbc(),
dsn = dsn,
username = keyring::key_list("database")[1,2],
password = keyring::key_get("database"))
}
Has anybody dealt with this? Why is the odbc Connections Contract not passed when wrapped inside of a function call? I am having trouble googling around for it. My idea was that the outcome of calling the code should work exactly the same whether or not its called within a function. Assigning a variable such as con to both types of executions (inside a function and outside a function) produce a global environment variable with identical attributes.
Thanks!