Monday, November 11, 2019

Lesson 7: Creating An RScript Header Template Snippet

Lesson 7: Creating An RScript Header Template Snippet

Introduction

Whenever I create an RScript, I usually try to put a “header” of information up at the top of the file that includes some basic information such as: the author’s name, the date, the name of the script, a brief description of the script, libraries, functions, etc.
I do my best to try to keep this “header” information in the same format everytime, but it can be tricky to get it just right each time.
Eventually, I decided to make a Rscript file that I would just use as a template and I would just open it whenever I started a new script and would just “save as…” a new script file.
Finally, I read an article from J.J. Allaire of RStudio on “RStudio’s Code Snippets”. After a little more research, I realized others were using code snippets to add header information to their new Rscripts. Dr. Timothy Farewell has a great post which I based my template and post on. Check his post out here.
This post is my take on the “Header Snippet”. Enjoy!

Step #1: Create Header Information

Below is the “header” template that I use for my RScripts. My header template is currently broken down into X sections: 1. Header a. Author b. Copyright with Year auto-populated c. Email address d. Today’s date auto-populated e. Script Name f. Script Description g. Notes 2. Working Directory 3. Options 4. Packages 5. Functions
This is just what I typically add when I’m setting up a new script, so the list above could be different for you and your needs. Feel free to adjust as for your specific needs.
# HEADER --------------------------------------------
#
# Author: RDataGuy
# Copyright (c) RDataGuy, `r paste(format(Sys.Date(), "%Y"))`
# Email:  rdataguy@gmail.com
# 
# Date: `r paste(Sys.Date())`
#
# Script Name:
#
# Script Description:
#
#
# Notes:
#
#

# SET WORKING DIRECTORY -----------------------------
cat("SETTING WORKING DIRECTORY...\n\n", sep = "")
wd <- "/cloud/project"
setwd(wd)
cat("WORKING DIRECTORY HAS BEEN SET TO: ", wd, sep = "")


# SET OPTIONS ---------------------------------------
cat("SETTING OPTIONS... \n\n", sep = "")
options(scipen = 999) # turns off scientific notation
options(encoding = "UTF-8") # sets string encoding to UTF-8 instead of ANSI


# INSTALL PACKAGES & LOAD LIBRARIES -----------------
cat("INSTALLING PACKAGES & LOADING LIBRARIES... \n\n", sep = "")
packages <- c("tidyverse", "stringr", "readxl") # list of packages to load
n_packages <- length(packages) # count how many packages are required

new.pkg <- packages[!(packages %in% installed.packages())] # determine which packages aren't installed

# install missing packages
if(length(new.pkg)){
  install.packages(new.pkg)
}

# load all requried libraries
for(n in 1:n_packages){
  cat("Loading Library #", n, " of ", n_packages, "... Currently Loading: ", packages[n], "\n", sep = "")
  lib_load <- paste("library(\"",packages[n],"\")", sep = "") # create string of text for loading each library
  eval(parse(text = lib_load)) # evaluate the string to load the library
}


# LOAD FUNCTIONS ------------------------------------
# space reserved for your functions
#

Step #2: Add Header To Snippets Library

After you’ve adjusted your template to meet your needs, then you’re ready to add the header template to your RStudio code snippets. To do this go to: “Tools” menu -> “Global Options…” -> “Code” -> “Editing” -> “Snippets” -> “Edit Snippets…”


Once you are in the “Edit Snippets…” screen, scroll to the bottom of the list of snippets and create a new line and then a new snippet by adding the following “snippet my_header”. Below that new line you just created paste all the header template code we created above and ensure the code is indented by one tab (see below).

Click “Save”, then “OK” to save the snippet and start using it in your scirpt.

Step #3: Wrapping It All Up

To use your snippet, simply create a new RScript file and then type “my_header” on the very first line of your new scrip and RStudio will auto-complete with a preview of your snippet code. Hit enter when the preview of your snippet code shows-up and RStudio will drop in all that code you specified in the “my_header” snippet.

That’s it!

I hope you learned something. If you have any questions, feel free to reach out.

Have fun coding!

RDataGuy


Download the code for this lesson below!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.