Skip to content

Instantly share code, notes, and snippets.

@francisbarton
Last active January 24, 2022 02:02
Show Gist options
  • Save francisbarton/a5907ec17154a59d5c9058d64b3012f5 to your computer and use it in GitHub Desktop.
Save francisbarton/a5907ec17154a59d5c9058d64b3012f5 to your computer and use it in GitHub Desktop.
My RStudio snippets
# https://gist.github.com/francisbarton/a5907ec17154a59d5c9058d64b3012f5
# I use this pattern regularly in chatty packages
snippet inf
usethis::ui_info(
stringr::str_glue("")
)
# set up a testthat block
snippet testt
"${1:test_name}" %>%
testthat::test_that({
testthat::expect_identical(
x,
y
)
})
# build and install from console
snippet bui
devtools::install(build = FALSE, upgrade = "never")
# facilitates the setting up of a roxygen data documentation skeleton with variables already named...
# useful when you have a wide data frame with lots of variables!
snippet docu
${1:data_object} %>%
names() %>%
paste0("\\item{", ., "}{}") %>%
stringr::str_c(collapse = "\n#' ") %>%
writeClipboard()
# easily copy last value to clipboard (better as an add-on?)
snippet cl
writeClipboard(.Last_value)
# save an object to an Rds file of the same name
snippet sv
saveRDS(${1:object}, here::here("rds_data", "${1:object}.Rds"))
# load an object from an Rds file
snippet rd
${1:object} <- readRDS(here::here("rds_data", "${1:object}.Rds"))
# stage everything to git
snippet add
gert::git_add(".")
# git commit
snippet comm
gert::git_commit_all(message = "${1:message}")
# nicely formatted date
snippet dt
`r format(Sys.Date(), "%B %d, %Y")`
# timestamp
snippet ts
`r paste("#", date(), "------------------------------\n")`
# my postcode regex (it'll do)
snippet postcode_regex
"[:alpha:]{1,2}[:digit:]{1,2}[:alnum:]?\\\\s?[:digit:]{1}[:alpha:]{2}$"
# regex look ahead
snippet la
"(?=${1})"
# regex look behind
snippet lb
"(?<=${1})"
# local folder for pinned data
snippet co_pin
pins::board_folder("C:/Users/Francis/Citizens Online/Citizens Online Team Site - Shared Documents/QGIS/co_data_pinboard")
# for some reason I hate typing this one out :-/
snippet rl
rlang::last_error()
snippet rt
rlang::last_trace()
snippet lib
library(${1:package})
snippet mat
matrix(${1:data}, nrow = ${2:rows}, ncol = ${3:cols})
snippet if
if (${1:condition}) {
${0}
}
snippet els
else {
${0}
}
snippet fun
${1:name} <- function(${2:variables}) {
${0}
}
snippet whi
while (${1:condition}) {
${0}
}
snippet swi
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})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment