Skip to content

Instantly share code, notes, and snippets.

@wch
wch / memleak-basic.R
Last active Mar 25, 2021
cpp11 memory leak
View memleak-basic.R
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)'
# ==============================================================================
View keybase.md

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 Nov 13, 2020
rlang::new_function() and rlang::as_function() with nested quosures
View rlang_function.R
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 Oct 22, 2020
Memoize async functions
View memoise_async.R
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 Sep 11, 2020
Memoize performance tests
View memoize.R
library(profvis)
library(microbenchmark)
library(memoise)
library(digest)
# Bare function
f <- function(a, b) {
a + b
}
@wch
wch / webapp.R
Created May 28, 2020
httpuv test app and curl fetch, in the same process
View webapp.R
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",
@wch
wch / install_logs.txt
Created Mar 26, 2020
Rcpp and httpuv install logs
View install_logs.txt
> install.packages("Rcpp")
Installing package into ‘/Users/winston/R/3.6’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/bin/macosx/el-capitan/contrib/3.6/Rcpp_1.0.4.tgz'
Content type 'application/x-gzip' length 3124401 bytes (3.0 MB)
==================================================
downloaded 3.0 MB
The downloaded binary packages are in
@wch
wch / 00README.md
Created Feb 15, 2020
Calling R function from C
View 00README.md

This is an example of C code calling an R function, by building a function call and then evaluating it.

@wch
wch / find_r_ver.R
Created Sep 16, 2019
Script for finding R version dependency
View find_r_ver.R
# First, need to install crandb
# devtools::install_github('r-hub/crandb')
# Memoize crandb::package because we end up making a lot of repetitive calls.
package <- memoise::memoise(crandb::package)
# Get a list of built-in packages. We don't need to check the version
# information for these packages.
builtin_pkgs <- memoise::memoise(function() {
pkgs <- installed.packages()
@wch
wch / remote_repl.R
Last active Aug 7, 2019
Remote R REPL app with httpuv
View remote_repl.R
library(httpuv)
PORT <- 7000
app <- list(
call = function(req) {
page(req)
},
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {