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
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) {
@wch
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
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
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
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
wch / promise_interrupts.R
Last active Mar 7, 2019
Promise chains and interrupts
View promise_interrupts.R
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 Feb 26, 2019
httpuv server that prints out headers
View server.R
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 Feb 14, 2019
withRestarts example
View with_restarts.R
f <- function() {
stop("foo")
}
withCallingHandlers(
{
withRestarts({
f()
1
},
@wch
wch / fork-github.md
Last active Aug 23, 2018
How to fork a GitHub repo and work on your fork
View fork-github.md

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.

You can’t perform that action at this time.