R
Templates¶
# ============================================================================ #
# 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:
if (!require("pacman")) {
install.packages("pacman")
library(pacman)
}
p_load(
tidyverse,
)
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:
-
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:
-
What are the differences between R's native pipe
|>and the magrittr pipe%>%? - Stack OverflowMany differences and limitations disappear when using |> in combination with an (anonymous) function.
-
Differences between the base R and magrittr pipes
R 4.2.0 added a
_placeholder to the base pipe, with one additional restriction: the argument has to be named. For example,x |> f(1, y = _)is equivalent tof(1, y = x).%>%allows you to drop the parentheses when calling a function with no other arguments;|>always requires the parentheses.
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) { }
Also see: