R/rmd/qmd Files

It is advisable to do followings at the beginning of each code file

Create Project

When wanting to write R code, create a project then add R/Rmd/qmd files into it instead of only creating a single file. This will allow saving the workspace settings.

Clean Workspace

Always start by clearing the workspace to ensure objects created in other files are not used in the current file by using the code shown blew. This can be done manaully in RStudio: Environment window -> Broom icon.

rm(list = ls())

Insall & Load Packages

Although people may not like installing packages on their machines automatically for different reasons, I prefer this route for myself because I use multiple machines and install packages one at a time (after the code breaks multiple time because of the missing packages) is annoying.

Step 1: Store all needed packages to a variables:

packages <- c("here", "tidyverse", "janitor")

Step 2: Install missing packages, if any

install.packages(setdiff(packages, rownames(installed.packages())))

Step 3: Load packages in one go

lapply(packages, require, character.only = TRUE)