Starting your R scripts in a common way

Keeping it consistent

R
RStudio
Author

Nick Twort

Published

16 Aug 2023

I always find it useful to start my R scripts in a consistent way, and in RStudio I have a snippet for this.

In your console, run usethis::edit_rstudio_snippets() and paste in the snippet below. Then you can execute it with hi Shift Tab.


snippet hi
    #--- Script details ------------------------------------------------------------
    # Creation date: `r paste(format(Sys.Date(), "%d %B %Y"))`
    # Client:        ${1:client}
    # Project:       ${2:`r gsub(pattern = "\\.Rproj", replacement = "", x = grep(pattern = "\\.Rproj", dir(), value = TRUE))`}
    # Description:   ${3:script description}
    # Author:        ${4:Nick Twort}
    
    library(tidyverse)
    library(lubridate)
    library(magrittr)
    library(here)
    library(janitor)

    #--- Import data ---------------------------------------------------------------
    
    ${0}