Skip to content

Instantly share code, notes, and snippets.

@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])))
}
@krlmlr
krlmlr / columns.R
Last active June 15, 2022 11:12
Script to query information_schema.columns with DBI
# Connect to your database here
con <- DBI::dbConnect(duckdb::duckdb())
# I'd be especially interested in getting a dump of this data!
columns <- DBI::dbGetQuery(con, "SELECT * FROM information_schema.columns WHERE LOWER(table_schema) = 'information_schema'")
columns
write.csv(columns, "columns.csv", na = "")
@krlmlr
krlmlr / columns.R
Last active June 15, 2022 02:58
Script to query information_schema.columns with dm >= 0.2.8.9007
library(dm)
# Connect to your database here
con <- DBI::dbConnect(duckdb::duckdb())
meta <- dm:::dm_meta(con, simple = TRUE)
# Does this work?
meta %>%
dm_ptype()
@krlmlr
krlmlr / dt-print.R
Last active April 24, 2022 12:59
Use tibble's print method for data.table
penguins_dt <- data.table::as.data.table(palmerpenguins::penguins[1:3,])
penguins_dt
print_data_table <- function(x, ...) {
# Adapted from data.table:::as.data.frame.data.table()
ans <- x
attr(ans, "row.names") <- .set_row_names(nrow(x))
attr(ans, "class") <- c("tbl", "data.frame")
attr(ans, "sorted") <- NULL
attr(ans, ".internal.selfref") <- NULL

Keybase proof

I hereby claim:

  • I am krlmlr on github.
  • I am krlmlr (https://keybase.io/krlmlr) on keybase.
  • I have a public key ASBWEsk3aAEpYhxYHUb4WzW1oPyWabWULdGBQ1_ci84Hiwo

To claim this, I am signing this object:

@krlmlr
krlmlr / block.R
Created June 10, 2021 03:40
Block assigning to a variable more than once
`<-` <- function(lhs, rhs, envir = parent.frame()) {
assign(as.character(substitute(lhs)), rhs, envir)
lockBinding(substitute(lhs), envir)
invisible(rhs)
}
@krlmlr
krlmlr / covr-per-file-analyze.R
Last active March 12, 2020 09:09
Mismatch between source and test file names
#' ---
#' title: Mismatch between source and test files in tibble
#' output:
#' html_notebook:
#' toc: true
#' code_folding: hide
#' ---
# For paged output
options(max.print = 1000)
library(RSQLite)
conn <- dbConnect(SQLite())
dbExecute(conn, "
CREATE TABLE IF NOT EXISTS `sim` (
`time` TEXT,
`subject` INTEGER,
`encounter` INTEGER,
`location` INTEGER,
`temp` REAL,