Created
January 9, 2026 09:02
-
-
Save jrosell/f6c68bb670c3c92767dc50224a01e2e8 to your computer and use it in GitHub Desktop.
R snippets for code autocompletion. Copy-paste-modify cases are good snippets candidates.
This file contains hidden or 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
| { | |
| "lib": { | |
| "prefix": "lib", | |
| "body": "library(${1:package})", | |
| "description": "Attach an R package" | |
| }, | |
| "src": { | |
| "prefix": "src", | |
| "body": "source(\"${1:file.R}\")", | |
| "description": "Source an R file" | |
| }, | |
| "ret": { | |
| "prefix": "ret", | |
| "body": "return(${1:code})", | |
| "description": "Return a value from a function" | |
| }, | |
| "mat": { | |
| "prefix": "mat", | |
| "body": "matrix(${1:data}, nrow = ${2:rows}, ncol = ${3:cols})", | |
| "description": "Define a matrix" | |
| }, | |
| "sg": { | |
| "prefix": "sg", | |
| "body": [ | |
| "setGeneric(\"${1:generic}\", function(${2:x, ...}) {", | |
| "\tstandardGeneric(\"${1:generic}\")", | |
| "})" | |
| ], | |
| "description": "Define a generic" | |
| }, | |
| "sm": { | |
| "prefix": "sm", | |
| "body": [ | |
| "setMethod(\"${1:generic}\", ${2:class}, function(${2:x, ...}) {", | |
| "\t${0}", | |
| "})" | |
| ], | |
| "description": "Define a method for a generic function" | |
| }, | |
| "sc": { | |
| "prefix": "sc", | |
| "body": "setClass(\"${1:Class}\", slots = c(${2:name = \"type\"}))", | |
| "description": "Define a class definition" | |
| }, | |
| "if": { | |
| "prefix": "if", | |
| "body": [ | |
| "if (${1:condition}) {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Conditional expression" | |
| }, | |
| "el": { | |
| "prefix": "el", | |
| "body": [ | |
| "else {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Conditional expression" | |
| }, | |
| "ei": { | |
| "prefix": "ei", | |
| "body": [ | |
| "else if (${1:condition}) {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Conditional expression" | |
| }, | |
| "fun": { | |
| "prefix": "fun", | |
| "body": [ | |
| "${1:name} <- function(${2:variables}) {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Function skeleton" | |
| }, | |
| "for": { | |
| "prefix": "for", | |
| "body": [ | |
| "for (${1:variable} in ${2:vector}) {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Define a loop" | |
| }, | |
| "while": { | |
| "prefix": "while", | |
| "body": [ | |
| "while (${1:condition}) {", | |
| "\t${0}", | |
| "}" | |
| ], | |
| "description": "Define a loop" | |
| }, | |
| "switch": { | |
| "prefix": "switch", | |
| "body": [ | |
| "switch (${1:object},", | |
| "\t${2:case} = ${3:action}", | |
| ")" | |
| ], | |
| "description": "Define a switch statement" | |
| }, | |
| "apply": { | |
| "prefix": "apply", | |
| "body": "apply(${1:array}, ${2:margin}, ${3:...})", | |
| "description": "Use the apply family" | |
| }, | |
| "lapply": { | |
| "prefix": "lapply", | |
| "body": "lapply(${1:list}, ${2:function})", | |
| "description": "Use the apply family" | |
| }, | |
| "sapply": { | |
| "prefix": "sapply", | |
| "body": "sapply(${1:list}, ${2:function})", | |
| "description": "Use the apply family" | |
| }, | |
| "mapply": { | |
| "prefix": "mapply", | |
| "body": "mapply(${1:function}, ${2:...})", | |
| "description": "Use the apply family" | |
| }, | |
| "tapply": { | |
| "prefix": "tapply", | |
| "body": "tapply(${1:vector}, ${2:index}, ${3:function})", | |
| "description": "Use the apply family" | |
| }, | |
| "vapply": { | |
| "prefix": "vapply", | |
| "body": "vapply(${1:list}, ${2:function}, FUN.VALUE = ${3:type}, ${4:...})", | |
| "description": "Use the apply family" | |
| }, | |
| "rapply": { | |
| "prefix": "rapply", | |
| "body": "rapply(${1:list}, ${2:function})", | |
| "description": "Use the apply family" | |
| }, | |
| "ts": { | |
| "prefix": "ts", | |
| "body": "`r paste(\"#\", date(), \"------------------------------\\n\")`", | |
| "description": "Insert a datetime" | |
| }, | |
| "shinyapp": { | |
| "prefix": "shinyapp", | |
| "body": [ | |
| "library(shiny)", | |
| "", | |
| "ui <- fluidPage(", | |
| " ${0}", | |
| ")", | |
| "", | |
| "server <- function(input, output, session) {", | |
| " ", | |
| "}", | |
| "", | |
| "shinyApp(ui, server)" | |
| ], | |
| "description": "Define a Shiny app" | |
| }, | |
| "shinymod": { | |
| "prefix": "shinymod", | |
| "body": [ | |
| "${1:name}_UI <- function(id) {", | |
| " ns <- NS(id)", | |
| " tagList(", | |
| "\t${0}", | |
| " )", | |
| "}", | |
| "", | |
| "${1:name} <- function(input, output, session) {", | |
| " ", | |
| "}" | |
| ], | |
| "description": "Define a Shiny module" | |
| }, | |
| "pak": { | |
| "prefix": "pak", | |
| "body": [ | |
| "stopifnot(requireNamespace(\"rlang\"))", | |
| "rlang::check_installed(\"pak\")", | |
| "pkgs <- rlang::chr(", | |
| " \"dplyr\",", | |
| " \"stringr\",", | |
| " \"tibble\",", | |
| " \"tidyr\",", | |
| " \"purrr\",", | |
| " \"jrrosell\" = \"jrosell/jrrosell\",", | |
| ")", | |
| "pak::pak(pkgs)", | |
| "libs <- ifelse(names(pkgs) == \"\", pkgs, names(pkgs))", | |
| "libs <- if (length(libs) == 0) pkgs else libs", | |
| "lapply(libs, library, quiet = TRUE, character.only = TRUE) |> invisible()" | |
| ], | |
| "description": "Install and load packages with pak" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment