-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path00-initial connection.R
More file actions
32 lines (25 loc) · 944 Bytes
/
00-initial connection.R
File metadata and controls
32 lines (25 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 1 - connect to the database ---------------------------------------------
# uses DBI and odbc packages - gives you view within R
library(DBI)
library(odbc)
# I use a function from a package I made (will error if on diff machine)
DPMmicrosim:::get_sql_con()
# under the hood it is this
get_sql_con <- function (server = NA)
{
if (is.na(server)) {
server = Sys.getenv("Server")
if(server==""){stop("need to have a server name")}
}
sql_con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = server,
Database = "MODELLING_SQL_AREA",
Trusted_Connection = "True",
timeout = 120)
return(sql_con)
}
# the Server name is saved in my .Renviron file so I don't have to commit it
# as code.
# to change your .Renviron file try usethis::edit_r_environ()
sql_con <- get_sql_con()