Last active
July 3, 2023 18:09
-
-
Save tessam30/fc775a2f917ea5d62de0f6724c4aeada to your computer and use it in GitHub Desktop.
R studio snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
snippet lib | |
library(${1:package}) | |
snippet req | |
require(${1:package}) | |
snippet src | |
source("${1:file.R}") | |
snippet ret | |
return(${1:code}) | |
snippet mat | |
matrix(${1:data}, nrow = ${2:rows}, ncol = ${3:cols}) | |
snippet sg | |
setGeneric("${1:generic}", function(${2:x, ...}) { | |
standardGeneric("${1:generic}") | |
}) | |
snippet sm | |
setMethod("${1:generic}", ${2:class}, function(${2:x, ...}) { | |
${0} | |
}) | |
snippet sc | |
setClass("${1:Class}", slots = c(${2:name = "type"})) | |
snippet if | |
if (${1:condition}) { | |
${0} | |
} | |
snippet el | |
else { | |
${0} | |
} | |
snippet ei | |
else if (${1:condition}) { | |
${0} | |
} | |
snippet fun | |
${1:name} <- function(${2:variables}) { | |
${0} | |
} | |
snippet for | |
for (${1:variable} in ${2:vector}) { | |
${0} | |
} | |
snippet while | |
while (${1:condition}) { | |
${0} | |
} | |
snippet switch | |
switch (${1:object}, | |
${2:case} = ${3:action} | |
) | |
snippet apply | |
apply(${1:array}, ${2:margin}, ${3:...}) | |
snippet lapply | |
lapply(${1:list}, ${2:function}) | |
snippet sapply | |
sapply(${1:list}, ${2:function}) | |
snippet mapply | |
mapply(${1:function}, ${2:...}) | |
snippet tapply | |
tapply(${1:vector}, ${2:index}, ${3:function}) | |
snippet vapply | |
vapply(${1:list}, ${2:function}, FUN.VALUE = ${3:type}, ${4:...}) | |
snippet rapply | |
rapply(${1:list}, ${2:function}) | |
snippet ts | |
`r paste("#", date(), "------------------------------\n")` | |
snippet shinyapp | |
library(shiny) | |
ui <- fluidPage( | |
${0} | |
) | |
server <- function(input, output, session) { | |
} | |
shinyApp(ui, server) | |
snippet shinymod | |
${1:name}_UI <- function(id) { | |
ns <- NS(id) | |
tagList( | |
${0} | |
) | |
} | |
${1:name} <- function(input, output, session) { | |
} | |
snippet ohab "base si packages" | |
library(gagglr) | |
library(tidyverse) | |
library(scales) | |
library(extrafont) | |
library(glue) | |
library(ggtext) | |
snippet ohaf "full si packages" | |
library(gagglr) | |
library(tidyverse) | |
library(scales) | |
library(sf) | |
library(extrafont) | |
library(tidytext) | |
library(patchwork) | |
library(ggtext) | |
library(glue) | |
library(gt) | |
library(gtExtras) | |
snippet nope | |
`%nope%` <- Negate(`%in%`) | |
snippet siei | |
scales::show_col(c("#2057a7", "#c43d4d", "#8980cb", "#e07653", "#1e87a5", "#f2bc40", "#287c6f", "#808080")) | |
snippet siei_pairs | |
scales::show_col(c("#2057a7", "#BFDDFF", "#c43d4d", "#FF939A", "#8980cb", "#DFD3FF", "#e07653", "#FFCAA2", "#1e87a5", "#83DBFB", "#f2bc40", "#FFDDA2", "#287c6f", "#7ECFC0", "#808080", "#E6E6E6")) | |
snippet proj_paths "standard paths" | |
data <- "Data" | |
dataout <- "Dataout" | |
images <- "Images" | |
graphs <- "Graphics" | |
snippet si_paths | |
merdata <- glamr::si_path("path_msd") | |
rasdata <- glamr::si_path("path_raster") | |
shpdata <- glamr::si_path("path_vector") | |
datim <- glamr::si_path("path_datim") | |
snippet lab "null ggplot labels" | |
labs(x = NULL, y = NULL, title = "${1}") | |
snippet msd "read psd" | |
df_msd <- read_msd(file_path) | |
snippet configs "load Zambia configs" | |
source("./Scripts/Z00_Config.R") | |
snippet spdfs "load spatial files" | |
source("./Scripts/Z01_fetch_spdfs.R") | |
snippet totnum "filter total numerator" | |
filter(standardizeddisaggregate == "Total Numerator", indicator %in% c("${1}")) | |
snippet sum "summing msd val" | |
summarise(total = sum(val, na.rm = TRUE)) | |
snippet rscript "script setup" | |
# PROJECT: ${1} | |
# PURPOSE: ${2:Munge and Analysis of} | |
# AUTHOR: ${3:Your Name | SI} | |
# REF ID: `r Sys.time() |> digest::sha1() |> substr(start = 1, stop = 8)` | |
# LICENSE: MIT | |
# DATE: `r Sys.Date()` | |
# NOTES: ${4} | |
# LOCALS & SETUP ============================================================================ | |
# Libraries | |
${5:ohaf} | |
# SI specific paths/functions | |
load_secrets() | |
merdata <- file.path(glamr::si_path("path_msd")) | |
file_path <- return_latest(folderpath = merdata, | |
pattern = "Site_IM_FY20-22_20220318_v2_1_Zambia") | |
# Grab metadata | |
get_metadata(file_path) | |
# REF ID for plots | |
ref_id <- "`r Sys.time() |> digest::sha1() |> substr(start = 1, stop = 8)`" | |
# Functions | |
# LOAD DATA ============================================================================ | |
${6:msd} | |
# MUNGE ============================================================================ | |
${7:#} | |
# VIZ ============================================================================ | |
${8:#} | |
# SPINDOWN ============================================================================ | |
snippet pri "prinf" | |
prinf()${0} | |
snippet gg "ggplot generic" | |
ggplot(aes(${2:aes})) + | |
${0} + | |
si_style() | |
snippet gb "ggplot bar graph" | |
ggplot(, aes(${2:aes})) + | |
geom_col() + | |
si_style_ygrid() + | |
scale_y_continuous(labels = comma)${0} | |
snippet gl "ggplot line" | |
ggplot(aes(${2:aes})) + | |
geom_line() + | |
geom_point(shape = 21, color = "white") + | |
scale_y_continuous(labels = comma) + | |
si_style_ygrid()${0} | |
snippet gheat "ggplot heatmap" | |
ggplot(aes(${2:aes, group = })) + | |
geom_tile(color = "white", size = 0.5) + | |
geom_text(aes(${3:label = ""})) + | |
si_fill_si(palette = "denims", discrete = FALSE) + | |
si_style_nolines()${0} | |
snippet gmap "ggplot map" | |
ggplot(aes(${2:aes})) + | |
geom_sf(aes(${3}), color = "white", size = 0.5) + | |
scale_fill_si(palette = "denims", | |
discrete = FALSE, | |
na.value = grey10k) + | |
si_style_map()${0} | |
snippet update_si "check for updates to si packages" | |
purrr::map(list("glitr", "glamr", "gisr", "gophr", "selfdestructin5"), ~devtools::install_github(paste0("USAID-OHA-SI/", ., "")))${0} | |
snippet la | |
"(?=${1})" | |
snippet lb | |
"(?<=${1})" | |
snippet stddisg "disagg" | |
standardizeddisaggregate | |
snippet oneline | |
labeller = label_wrap_gen(multi_line=FALSE) | |
snippet fragment | |
[${1:text}]{.${2:type}} | |
snippet aside | |
[${1:text}]{.aside} | |
snippet fence | |
:::{.${1:type}} | |
${2:body} | |
::: | |
snippet panel | |
::: {.panel-tabset} | |
${1:body} | |
::: | |
snippet column | |
:::: {.columns} | |
::: {.column width="50%"} | |
Left column | |
::: | |
::: {.column width="50%"} | |
Right column | |
::: | |
:::: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment