Skip to content

R

Templates

A Minimal R Script Template
# ============================================================================ #
# Script: <script name>.R
# Description: <description of what this script does>
# ============================================================================ #

# ============================================================================ #
# Set up ----
# ============================================================================ #

## Source libraries ----
if (!require("pacman")) {
  install.packages("pacman")
  library(pacman)
} 
p_load(
  tidyverse
)

## Define paths ----
# input_path <- "../input"
# output_path <- "../output"
# lib_path <- "../../lib"
# temp_path <- "../temp"

## Source other scripts ----
# source()

## Define global data variables ----

# ============================================================================ #
# Define Functions ----
# Note: Factorize when the code stabilizes 
# ============================================================================ #

# main <- function() {
# 
# }
# 
# main()

# ============================================================================ #
# 1. Load data ----
# ============================================================================ #

# ============================================================================ #
# 2. Section 2 ----
# ============================================================================ #

# ============================================================================ #
# 3. Section 3 ----
# ============================================================================ # 

Snippets

Loading packages:

Using `pacman`
if (!require("pacman")) {
  install.packages("pacman")
  library(pacman)
} 
p_load(
  tidyverse,
)
Manual loop
packages <- c("tidyverse")
for (pkg in packages) {
  if (!require(pkg, character.only = TRUE, quietly = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

R Studio

Importing data

Syntax

Pipe

Resources:

  • 18 Pipes | R for Data Science

    Pipes are most useful for rewriting a fairly short linear sequence of operations. I think you should reach for another tool when: Your pipes are longer than (say) ten steps... You have multiple inputs or outputs... You are starting to think about a directed graph with a complex dependency structure.

magrittr pipes:

base R pipe

base R pipe vs. magrittr pipe:

Functions

Anonymous function

Inline documentation

  • In-Line Documentation for R โ€ข roxygen2

    Example
    #' The length of a string
    #'
    #' Technically this returns the number of "code points", in a string. One
    #' code point usually corresponds to one character, but not always. For example,
    #' an u with a umlaut might be represented as a single character or as the
    #' combination a u and an umlaut.
    #'
    #' @inheritParams str_detect
    #' @return A numeric vector giving number of characters (code points) in each
    #'    element of the character vector. Missing string have missing length.
    #' @seealso [stringi::stri_length()] which this function wraps.
    #' @export
    #' @examples
    #' str_length(letters)
    #' str_length(NA)
    #' str_length(factor("abc"))
    #' str_length(c("i", "like", "programming", NA))
    str_length <- function(string) {
    }
    
  • Documenting functions โ€ข roxygen2

Also see:

Unit testing