Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
@wch
wch / pluck_recursive.R
Created March 10, 2022 04:21
From a tree, recursively pluck all elements with a particular name
# @drob's version from https://twitter.com/drob/status/1501747414780239879
pluck_recursive <- function(lst, name) {
if (is.list(lst)) {
if (!is.null(lst[[name]])) {
return(lst[[name]])
}
return(unname(unlist(purrr::map(lst, pluck_recursive, name))))
}
}
@wch
wch / mtime.R
Last active November 9, 2021 19:20
C function to get mtime of a file, from R
library(inline)
get_mtime <- cfunction(
signature(filename = "character"),
includes = "
#include <time.h>
#include <sys/stat.h>",
body = '
const char* path = CHAR(asChar(filename));
struct stat attr;
stat(path, &attr);
@wch
wch / contextvars.py
Last active August 10, 2021 17:09
ContextVars demonstration
# Demonstration of using ContextVars instead of global variables.
# Set to True for global mode, False for ContextVar mode
global_mode = False
# With a global variable, the result is: 1 2 3 3 3 3 3 3 3
# With a ContextVar, the result is: 1 2 3 1 2 3 1 2 3
import asyncio
import contextvars
@wch
wch / later_test.R
Last active June 25, 2021 20:42
later callback execution order bug
# This code uses later's C API to schedule callbacks as quickly as possible, and
# it tests if the callbacks execute in the same order that they are scheduled.
# In Docker on Mac, they sometimes do not.
docker run --rm -ti rocker/shiny /bin/bash
R
install.packages(c('cpp11', 'later', 'brio', 'callr', 'cli', 'decor', 'desc',
'tibble', 'vctrs'))
library(cpp11)
@wch
wch / memleak-basic.R
Last active March 25, 2021 14:40
cpp11 memory leak
docker run --rm -ti --security-opt seccomp=unconfined wch1/r-debug
# =====================================================================
# Setup
# =====================================================================
RD -e 'install.packages(c("cpp11", "decor", "igraph"))'
RD -e 'remotes::install_github("r-lib/memtools", build_manual = FALSE, build_vignettes = FALSE)'
# ==============================================================================

Keybase proof

I hereby claim:

  • I am wch on github.
  • I am winston (https://keybase.io/winston) on keybase.
  • I have a public key ASBIcD-b7Mr5y30DdA4AfVp_I263JfDKgLboP2DYCHpfvwo

To claim this, I am signing this object:

@wch
wch / rlang_function.R
Last active November 13, 2020 19:08
rlang::new_function() and rlang::as_function() with nested quosures
library(rlang)
library(pryr)
# ===========================
# Normal quosures
# ===========================
# as_function() with a quosure: OK
a <- 1
x <- quo(a + 10)
a <- 2
@wch
wch / memoise_async.R
Created October 22, 2020 23:00
Memoize async functions
library(digest)
library(promises)
library(cache)
# Given a list with items named `value` and `visible`, return `x$value` either
# visibly, or invisibly, depending on the value of `x$visible`.
valueWithVisible <- function(x) {
if (x$visible) x$value else invisible(x$value)
}
@wch
wch / memoize.R
Created September 11, 2020 17:56
Memoize performance tests
library(profvis)
library(microbenchmark)
library(memoise)
library(digest)
# Bare function
f <- function(a, b) {
a + b
}
@wch
wch / webapp.R
Created May 28, 2020 21:34
httpuv test app and curl fetch, in the same process
library(httpuv)
# Create a web server app which returns the time and prints out the value of
# a header named "test-header".
handle_request <- function(req) {
list(
status = 200L,
headers = list('Content-Type' = 'text/plain'),
body = paste0(
"The time is: ", Sys.time(), "\n",