Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
@wch
wch / remote_repl.R
Last active August 7, 2019 17:42
Remote R REPL app with httpuv
library(httpuv)
PORT <- 7000
app <- list(
call = function(req) {
page(req)
},
onWSOpen = function(ws) {
ws$onMessage(function(binary, message) {
@wch
wch / modify_vector.R
Created July 10, 2019 15:55
Modifying a vector in an object
## 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
wch / symbol_table.R
Last active July 30, 2019 19:34
Get contents of R symbol table
# 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
wch / thisfile.R
Created April 11, 2019 00:09
Function for detecting the script that's being run
# 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
wch / gist:255058e9639992a28573a3bcb9cdfcd4
Created March 19, 2019 17:15
Commands for reproducing CRAN r-devel-linux-x86_64-debian-gcc compiler warnings with Docker
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
wch / promise_interrupts.R
Last active March 7, 2019 16:53
Promise chains and interrupts
library(promises)
library(later)
# 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
wch / server.R
Created February 26, 2019 17:41
httpuv server that prints out headers
library(httpuv)
s <- startServer("0.0.0.0", 5000,
list(
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")
@wch
wch / with_restarts.R
Created February 14, 2019 03:47
withRestarts example
f <- function() {
stop("foo")
}
withCallingHandlers(
{
withRestarts({
f()
1
},
@wch
wch / fork-github.md
Last active January 5, 2025 00:47
How to fork a GitHub repo and work on your fork

This document shows how to fork a GitHub repo and work from your fork to create clean branches and pull requests. The key is to make sure you stay in sync with the upstream fork's master branch.

Setup

First, fork the repo by clicking the Fork button on GitHub. For example, you could fork the tidyverse/ggplot2 repo at https://github.com/tidyverse/ggplot2.

@wch
wch / time_check.R
Last active September 17, 2018 03:52
Function for checking what operations modify ctime/atime/mtime
time_check <- function(filename) {
times_df <- function(filename, label) {
times <- file.info(filename)[c('mtime', 'ctime', 'atime')]
as.data.frame(list(label = label, as.list(times)))
}
unlink(filename)
file.create(filename)
times <- times_df(filename, "creation")