I am trying to connect to an ODBC database that's a defined DSN from within a function.
(To ultimate goal is to standardize the DB connection process within our team and log information about connections to an in-house log file.)
I thought this would be easy, but I'm having trouble getting it to work. (So now I'm stuck wanting to understand why!)
I've tried different variations, but I am trying something like this:
connectDB <- function() {
con <- DBI::dbConnect(odbc::odbc(), "MyDB")
assign("MyDB", con, envir = .GlobalEnv)
}
However, no matter what I try, it just creates a variable, not a connection. (I realize this is what "assign" does -- creates a variable; I saw a thread on stack exchange that said this would work. I have tried ways: open() -- since this needs to be a connection, and I've even tried using .valueClassTest() -- though I don't know enough to use this effectively.)
Any insight or help is appreciated.