Skip to content

Instantly share code, notes, and snippets.

@krlmlr
krlmlr / vec_slice2.R
Created August 15, 2024 18:19
vec_slice2() prototype
library(tidyverse)
vec_slice2 <- function(x, i) {
if (!is.data.frame(x)) {
return(.subset2(x, i))
}
# FIXME: Other special cases?
row <- vctrs::vec_slice(x, i)
lists <- map_lgl(row, ~ is.list(.x) && !is.data.frame(.x))
length_one_elements <- map_lgl(
@krlmlr
krlmlr / pak-duckdb.R
Created October 26, 2023 17:41
Install the duckdb R package from GitHub
pak::pak("duckdb/duckdb-r")
#> ! Using bundled GitHub PAT. Please add your own PAT using `gitcreds::gitcreds_set()`.
#>
#> ✔ Updated metadata database: 4.81 MB in 4 files.
#>
#> ℹ Updating metadata database
#> ✔ Updating metadata database ... done
#>
#>
#> → Will update 1 package.
facepalm <- "\U1F926"
fitzpatrick_3 <- "\U1F3FC"
zero_width_joiner <- "\U200D"
male <- "\U2642"
my_facepalm <- paste0(facepalm, fitzpatrick_3, zero_width_joiner, male)
my_facepalm
#> [1] "🤦🏼‍♂"
# 😱
@krlmlr
krlmlr / dm-standalone-check_suggested.R
Created October 23, 2023 05:08
Using check_suggested() standalone
usethis::use_standalone("cynkra/dm", "standalone-check_suggested.R")
#> ✔ Setting active project to '/Users/kirill/git/R/fledge'
#> ✔ Writing 'R/import-standalone-check_suggested.R'
@krlmlr
krlmlr / check_installed.R
Created October 23, 2023 04:03
Demo for rlang::check_installed()
my_fun <- function() {
rlang::local_interactive()
rlang::check_installed(c("DiagrammeR (>= 1.0.9)", "DiagrammeRsvg"))
stopifnot(requireNamespace("DiagrammeR", quietly = TRUE))
stopifnot(requireNamespace("DiagrammeRsvg", quietly = TRUE))
}
my_fun()
#> ℹ The packages "DiagrammeR" (>= 1.0.9) and "DiagrammeRsvg" are required.
#> ✖ Would you like to install them?
@krlmlr
krlmlr / multi.R
Created October 20, 2023 05:37
Running multiple statements with DBI
library(DBI)
con <- dbConnect(
RPostgres::Postgres(),
...
)
dbExecute(
con,
"CREATE TEMP TABLE x (a int); CREATE TEMP TABLE y (b int)",
immediate = TRUE
@krlmlr
krlmlr / bracket.R
Created October 11, 2023 04:43
Comparing single- and double-bracket subsetting for atomic vectors
my_letters <- c("A", "B", "C", "D", "E")
bench::mark(for (i in 1:10000) NULL)
#> # A tibble: 1 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 for (i in 1:10000) NULL 312µs 332µs 2972. 38.9KB 36.3
bench::mark(for (i in 1:10000) my_letters[3], for (i in 1:10000) my_letters[[3]])
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:t> <bch:> <dbl> <bch:byt> <dbl>
@krlmlr
krlmlr / log.R
Created August 6, 2023 09:30
Call logging
# Use: add `log_call("<function name")` to your functions
# TODO: Add to all functions in a namespace via `trace()`
indent <- new_environment(list(value = 0L))
log_call <- function(name, call = sys.call(-1), envir = parent.frame()) {
srcref <- attr(call, "srcref")
if (is.null(srcref)) {
call_dep <- paste(gsub("^ +", "", deparse(call)), collapse = "")
@krlmlr
krlmlr / use_tests.R
Last active March 23, 2023 08:03
Create boilerplate test files from source files in an R package
library(conflicted)
library(tidyverse)
new_region <- function(name) {
paste0("# ", name, " ", strrep("-", max(88 - nchar(name) - 4, 0)))
}
boilerplate <- function(name) {
paste(collapse = "\n", c(
new_region(name),
@krlmlr
krlmlr / generate.R
Last active July 11, 2022 01:35
Generate all combinations with named and unnamed arguments for a call
generate <- function(named, unnamed, used = exprs()) {
if (length(named) == 0 && length(unnamed) == 0) {
return(deparse(call2("check_api", !!!used), width.cutoff = 500))
}
out <- character()
for (i in seq_along(named)) {
out <- c(out, generate(named[-i], unnamed, c(used, named[i])))
}