I have a postgresql database connection and want to get a table from the database. Presumably it's good practice to keep the connection info in a different file? I have two files just now:
#getthetable.R
library(tidyverse)
library(dbplyr)
## connect to db
con <- src_postgres(dbname = "thedbname",
host = "blablabla.amazonaws.com",
port = NULL,
user = "myname",
password = "1234")
thetable <- tbl(con, "thetable") %>% select(id, apples, carrots) %>% collect
And then:
#main.R
library(tidyverse)
## get data from getthetable script with connection
source("rscripts/getthetable.R")
This now makes both con
and thetable
variables available in main.R. I just want the variable thetable
from getthetable.R. How do I do that? Leaving out con variable?
Also, is there a best practice here when working with db connections in r? Is my thinking logical? Are there drawbacks to what I'm doing or do most people just put the connection in together with the main scripts?