Skip to content

Instantly share code, notes, and snippets.


Winston Chang wch

View GitHub Profile
wch / memoize.R
Created Sep 11, 2020
Memoize performance tests
View memoize.R
# Bare function
f <- function(a, b) {
a + b
wch / webapp.R
Created May 28, 2020
httpuv test app and curl fetch, in the same process
View webapp.R
# Create a web server app which returns the time and prints out the value of
# a header named "test-header".
handle_request <- function(req) {
status = 200L,
headers = list('Content-Type' = 'text/plain'),
body = paste0(
"The time is: ", Sys.time(), "\n",
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 ''
Content type 'application/x-gzip' length 3124401 bytes (3.0 MB)
downloaded 3.0 MB
The downloaded binary packages are in
wch /
Created Feb 15, 2020
Calling R function from C

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

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 / remote_repl.R
Last active Aug 7, 2019
Remote R REPL app with httpuv
View remote_repl.R
PORT <- 7000
app <- list(
call = function(req) {
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {
wch / modify_vector.R
Created Jul 10, 2019
Modifying a vector in an object
View modify_vector.R
## Modifying a vector in a parent environment
## This tests whether it's faster to use `self` or not, and whether it's faster to use `<-` vs `<<-`.
foo <- local({
x <- integer()
self <- environment()
# Four different ways of setting a value:
# * With and without using `self`
wch / symbol_table.R
Last active Jul 30, 2019
Get contents of R symbol table
View symbol_table.R
# get_symbols() returns all symbols that are registered in R's symbol table.
# new_symbols() returns all symbols that have been added since the last time
# new_symbols() was run. If you want to test whether your code causes the symbol
# table to grow, run new_symbols(), then run your code, then run new_symbols()
# again.
get_symbols <- inline::cfunction(
includes = "
#define HSIZE 49157 /* The size of the hash table for symbols */
wch / thisfile.R
Created Apr 11, 2019
Function for detecting the script that's being run
View thisfile.R
# This script can be sourced from RStudio, or run with Rscript.
# Returns the file currently being sourced or run with Rscript
this_file <- function() {
cmdArgs <- commandArgs(trailingOnly = FALSE)
needle <- "--file="
match <- grep(needle, cmdArgs)
if (length(match) > 0) {
# Rscript
return(normalizePath(sub(needle, "", cmdArgs[match])))
wch / gist:255058e9639992a28573a3bcb9cdfcd4
Created Mar 19, 2019
Commands for reproducing CRAN r-devel-linux-x86_64-debian-gcc compiler warnings with Docker
View gist:255058e9639992a28573a3bcb9cdfcd4
docker run --rm -ti rhub/debian-gcc-devel
echo -e "
#include <stdlib.h>
#include <string.h>
char* create_string(const char* value) {
// strlen doesn't count null terminator
int n = strlen(value) + 1;
char* str = (char *) malloc(n);
You can’t perform that action at this time.