Site icon R-bloggers

Creating an R Project Directory

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

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.

< section id="function" class="level1">

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.

if(!require(fs)) {
  install.packages("fs")
}
suppressPackageStartupMessages(library(fs))

Next we create a character vector of folder paths that will exist inside of the main project folder itself.

folders <- c(
  "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.

fs::dir_create(
  path = 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 
db_connect <- function() {
  db_con <- LICHospitalR::db_connect()
  
  return(db_con)
  
}

# Disconnect from Database
db_disconnect <- function(.connection) {
  
  DBI::dbDisconnect(
    conn = db_connect()
  )
  
}

Now, let’s load in the typical libraries. You can modify this to suit your own needs.

# Library Load

library_load <- function(){
  
  if(!require(pacman)){install.packages("pacman")}
  pacman::p_load(
    "DBI"
    , "odbc"
    , "janitor"
    , "dplyr"
    , "tibble"
    , "tidyr"
    , "LICHospitalR"
    , "modeltime"
  )
  
}

Ok so now the functions have been created, let’s dump them!

db_funs <- c("db_connect","db_disconnect")
dump(
  list = db_funs,
  file = "00_Scripts/db_con_obj.R"
)

lib_funs <- "library_load"
dump(
  list = lib_funs,
  file = "00_Scripts/library_load.R"
)
< section id="example" class="level1">

Example

Here is the full script!

if(!require(fs)) {
  install.packages("fs")
}
suppressPackageStartupMessages(library(fs))

folders <- c(
  "00_Scripts"
  , "00_Data"
  , "01_Queries"
  , "02_Data_Manipulation"
  , "03_Viz"
  , "04_TS_Modeling"
  , "99_Automations"
)

fs::dir_create(
  path = 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 
db_connect <- function() {
  db_con <- LICHospitalR::db_connect()
  
  return(db_con)
  
}

# Disconnect from Database
db_disconnect <- function(.connection) {
  
  DBI::dbDisconnect(
    conn = db_connect()
  )
  
}

# Library Load

library_load <- function(){
  
  if(!require(pacman)){install.packages("pacman")}
  pacman::p_load(
    "DBI"
    , "odbc"
    , "janitor"
    , "dplyr"
    , "tibble"
    , "tidyr"
    , "LICHospitalR"
    , "modeltime"
  )
  
}

db_funs <- c("db_connect","db_disconnect")
dump(
  list = db_funs,
  file = "00_Scripts/db_con_obj.R"
)

lib_funs <- "library_load"
dump(
  list = lib_funs,
  file = "00_Scripts/library_load.R"
)

Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.