Skip to content

Instantly share code, notes, and snippets.

@dpastoor
Created April 22, 2015 16:22
Show Gist options
  • Select an option

  • Save dpastoor/eac7bd90fb5ccb50024d to your computer and use it in GitHub Desktop.

Select an option

Save dpastoor/eac7bd90fb5ccb50024d to your computer and use it in GitHub Desktop.
R snippets for vim
snippet bp "Expand the shebang and add boilerplate text." b
###############################################################################
# Purpose: ${1:...}
# Author: ${2:...}
# Dependencies: ${3:...}
# Created: `date +%c`
###############################################################################
${0}
endsnippet
snippet mod "Add modification time to the boilerplate." b
# Modified: `date +%c` ${0}
endsnippet
snippet lib "Source lib.R" b
# Source packages and custom functions.
source("./lib.R")
${0}
endsnippet
snippet pm "Package management boilerplate" b
# Package management boilerplate
deps = c("${1:package}")
depsNA = deps[!deps %in% rownames(installed.packages())]
if (length(depsNA) > 0) install.packages(depsNA, dependencies=TRUE)
for (d in deps) library(d, character.only=TRUE)
rm(d, deps, depsNA)
${0}
endsnippet
snippet pkg "Add a library"
library(${1:library})
${0}
endsnippet
snippet req "Add a library using require()"
require(${1:library})
${0}
endsnippet
snippet rm "Clear current list of objects" b
rm(list=ls())
${0}
endsnippet
snippet if "One liner `small' if" b
if (${1:test} ${2:operator} ${3:boolean}) ${4:code}
${0}
endsnippet
snippet if "The if command" b
if (${1:x} ${2:==} ${3:0L}) {
${4:###}
}
${0}
endsnippet
snippet if "If else block" b
if (${1:test} ${2:operator} ${3:boolean}) {
${4:###}
} else {
${5:###}
}
${0}
endsnippet
snippet for "The `for' loop" b
for (${1:x} in ${2:s}) {
${4:###}
}
${0}
endsnippet
snippet while "The `while' loop" b
while (${1:x} ${2:==} ${3:0L}) {
${4:###}
}
${0}
endsnippet
snippet = "Gives an assigment statement" b
${1:foo} = ${2:bar}
${0}
endsnippet
snippet fun "Defines a new function" b
${1:verb}${2:Subject} = function (${3:args}) {
${4:###}
}
${0}
endsnippet
snippet fundoc "Defines a function with a docstring" b
${1:verb}${2:Subject} = function (${3:args}) {
# $1$2:
#
# ${4:Description}
#
# Args:
#
# Returns:
# $2: ${6:Description}
#
${7:###}
}
${0}
endsnippet
snippet app "apply" w
apply(X=${1:x}, MARGIN=${2:2},
FUN=${3:function (${4:k}) {
${5:###}
}})
${0}
endsnippet
snippet app "sapply" w
sapply(X=${1:x},
FUN=${2:function (${3:k}) {
${4:###}
}})
${0}
endsnippet
snippet app "lapply" w
lapply(X=${1:x},
FUN=${2:function (${3:k}) {
${4:###}
}})
${0}
endsnippet
snippet app "mapply" w
mapply(FUN=${1:function (${2:k}, ${3:l}${4:, ...}) {
${5:###}
}},
${6:x}, ${7:y}${8:,
${9:...}})
${0}
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment