Skip to content

Instantly share code, notes, and snippets.

@denisabd
Last active February 22, 2023 19:08
Show Gist options
  • Save denisabd/36fac7ca960a56eee3f5e3c6694febb7 to your computer and use it in GitHub Desktop.
Save denisabd/36fac7ca960a56eee3f5e3c6694febb7 to your computer and use it in GitHub Desktop.
RStudio Snippets
snippet shiny:module
${1:name}UI <- function(id) {
ns <- NS(id)
tagList(
${0}
)
}
${1:name}Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
}
)
}
snippet shiny:async module
${1:name}UI <- function(id) {
ns <- NS(id)
fluidRow(
${0}
)
}
${1:name}Server <- function(id, df) {
moduleServer(
id,
function(input, output, session) {
# Reactive Values
rv <- reactiveValues(
result = NULL
)
observeEvent(df(), {
df <- df()
promises::future_promise({
${0}
}) %...>%
(function(result){
# Success
}) %...!%
(function(error) {
# Error
print(error)
})
cli::cat_rule(paste("Ready", Sys.time()))
})
}
)
}
snippet database:database_connection
library(DBI)
${1:con} <- dbConnect(duckdb::duckdb(), "${2:db_name}")
dbListTables(${1:con})
dbWriteTable(conn = ${1:con},
name = __,
value = __,
overwrite = TRUE)
dbDisconnect(${1:con})
snippet tidy:get_measure
dplyr::ungroup() %>%
dplyr::select_if(is.numeric) %>%
names() %>%
magrittr::extract2(1)
snippet tidy:get_dimensions
dplyr::ungroup() %>%
dplyr::select(where(is.character), where(is.factor)) %>%
names()
snippet tidy:get_date
dplyr::ungroup() %>%
dplyr::select_if(lubridate::is.timepoint) %>%
names() %>%
magrittr::extract2(1)
snippet tidy:max_date
dplyr::ungroup() %>%
dplyr::select_if(lubridate::is.timepoint) %>%
as.matrix() %>%
as.Date(origin = "1970-01-01") %>%
max()
snippet tidy:min_date
dplyr::ungroup() %>%
dplyr::select_if(lubridate::is.timepoint) %>%
as.matrix() %>%
as.Date(origin = "1970-01-01") %>%
min()
snippet tidy:type_convert
dplyr::group_by() %>%
readr::type_convert(na = c("")) %>%
suppressMessages() %>%
suppressWarnings()
snippet shiny:future_promise
promises::future_promise({
}) %...>%
(function(result) {
# Success
}) %...!%
(function(error) {
# Error
print(error)
})
snippet assertion:dataframe
if (!is.data.frame(${1:df})) stop("'${1:df}' must be a data.frame or tibble")
if (nrow(${1:df}) == 0) stop("'${1:df}' must have at least one row, execution is stopped")
snippet assertion:numeric field
if (!is.numeric(${1:field})) stop("${1:field} must be numeric")
snippet assertion:date field
if (!lubridate::is.Date(${1:field})) stop("${1:field} must be a date")
snippet assertion:exists
if (!exists("${1:field}")) ${1:field} <- character()
snippet options
options(dplyr.summarise.inform = FALSE, scipen = 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment