Skip to content

Instantly share code, notes, and snippets.

Winston Chang wch

Block or report user

Report or block wch

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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);
wch / promise_interrupts.R
Last active Mar 7, 2019
Promise chains and interrupts
View promise_interrupts.R
# Regular version ===========
# Interrupts during execution of a step in the promise chain cause the chain to stop
# without running further steps, the catch, or finally.
# Ideally, the finally would run, but not the others.
p <- promise_resolve(TRUE)
wch / server.R
Created Feb 26, 2019
httpuv server that prints out headers
View server.R
s <- startServer("", 5000,
call = function(req) {
txt <- as.list(req$HEADERS)
txt <- paste(capture.output(str(txt)), collapse = "\n")
txt <- paste0(req$REQUEST_METHOD, " ", req$PATH_INFO, " \n", txt)
cat(txt, "\n\n")
You can’t perform that action at this time.