From ac060410c3b5337263f0ba1bdccab57d6f4aff60 Mon Sep 17 00:00:00 2001 From: Caiwen Li <130128348+LisaLi525@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:54:41 +0200 Subject: [PATCH] Create Setup and Database Connection.R --- Setup and Database Connection.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Setup and Database Connection.R diff --git a/Setup and Database Connection.R b/Setup and Database Connection.R new file mode 100644 index 0000000..973e58a --- /dev/null +++ b/Setup and Database Connection.R @@ -0,0 +1,15 @@ +# Clear all variables and set chunk options +setup_environment <- function() { + rm(list = ls()) + knitr::opts_chunk$set(echo = TRUE) +} + +# Create a connection to the database +connect_to_database <- function(host, port, user, password, dbname) { + library(RPostgreSQL) + library(dplyr) + library(dbplyr) + + db_connection <- src_postgres(dbname, host = host, port = port, user = user, password = password) + return(db_connection) +}