if(!require(fs)) {
install.packages("fs")
}suppressPackageStartupMessages(library(fs))
Introduction
When working in R I find it best to create a new project when working on something. This keeps all of the data and scripts in one location. This also means that if you are not careful the directory you have your project in can become quite messy. This used to happen to me with regularity, then I got smart and wrote a script that would standardize how projects are built for me.
I find it important to have different fodlers for different parts of a project. This does not mean I will use them all for every project but that is fine, you can either comment that portion out or just delete the files that are created.
Function
Here is what I do broken down into different steps. First, I see if the package {fs}
is installed, and if not, then install it, and finally load it.
Next we create a character vector of folder paths that will exist inside of the main project folder itself.
<- c(
folders "00_Scripts"
"00_Data"
, "01_Queries"
, "02_Data_Manipulation"
, "03_Viz"
, "04_TS_Modeling"
, "99_Automations"
, )
Now that the folders we want are spelt out, we can create them.
::dir_create(
fspath = folders
)
Now that is done, it’s off to creating a few files that I personally almost always use. I do a lot of work out of a data warehouse so a connection file is needed. We also need a disconnection function.
# DSS Connection
<- function() {
db_connect <- LICHospitalR::db_connect()
db_con
return(db_con)
}
# Disconnect from Database
<- function(.connection) {
db_disconnect
::dbDisconnect(
DBIconn = db_connect()
)
}
Now, let’s load in the typical libraries. You can modify this to suit your own needs.
# Library Load
<- function(){
library_load
if(!require(pacman)){install.packages("pacman")}
::p_load(
pacman"DBI"
"odbc"
, "janitor"
, "dplyr"
, "tibble"
, "tidyr"
, "LICHospitalR"
, "modeltime"
,
)
}
Ok so now the functions have been created, let’s dump them!
<- c("db_connect","db_disconnect")
db_funs dump(
list = db_funs,
file = "00_Scripts/db_con_obj.R"
)
<- "library_load"
lib_funs dump(
list = lib_funs,
file = "00_Scripts/library_load.R"
)
Example
Here is the full script!
if(!require(fs)) {
install.packages("fs")
}suppressPackageStartupMessages(library(fs))
<- c(
folders "00_Scripts"
"00_Data"
, "01_Queries"
, "02_Data_Manipulation"
, "03_Viz"
, "04_TS_Modeling"
, "99_Automations"
,
)
::dir_create(
fspath = folders
)
file_create("01_Queries/query_functions.R")
file_create("02_Data_Manipulation/data_functions.R")
file_create("03_Viz/viz_functions.R")
file_create("04_TS_Modeling/ts_functions.R")
# DSS Connection
<- function() {
db_connect <- LICHospitalR::db_connect()
db_con
return(db_con)
}
# Disconnect from Database
<- function(.connection) {
db_disconnect
::dbDisconnect(
DBIconn = db_connect()
)
}
# Library Load
<- function(){
library_load
if(!require(pacman)){install.packages("pacman")}
::p_load(
pacman"DBI"
"odbc"
, "janitor"
, "dplyr"
, "tibble"
, "tidyr"
, "LICHospitalR"
, "modeltime"
,
)
}
<- c("db_connect","db_disconnect")
db_funs dump(
list = db_funs,
file = "00_Scripts/db_con_obj.R"
)
<- "library_load"
lib_funs dump(
list = lib_funs,
file = "00_Scripts/library_load.R"
)
Voila!