Skip to content

Instantly share code, notes, and snippets.

@wch
wch / locale.R
Created December 18, 2015 15:31
Multibyte non-UTF-8 locales
# ==== Creating UTF-8 strings ====
# This is how to create a string with UTF-8 encoding. This should work
# regardless of the current locale settings.
x <- rawToChar(as.raw(c(0xe5, 0x8d, 0x88)))
Encoding(x) <- "UTF-8"
x
# [1] "午"
# Another string, 'Δ★😎'
pat <- rawToChar(as.raw(c(0xce, 0x94, 0xe2, 0x98, 0x85, 0xf0, 0x9f, 0x98, 0x8e)))
@wch
wch / multi_dispatch.R
Last active December 24, 2023 17:20
Multiple dispatch in R without S4
# ---- Multiple dispatch functions -----
multi_dispatch <- function(gen_name) {
calling_env <- parent.frame()
parent_call <- sys.call(sys.parent())
calling_fun <- sys.function(sys.parent())
arg1 <- eval(parent_call[[2]], calling_env)
arg2 <- eval(parent_call[[3]], calling_env)
@wch
wch / pairlist.R
Last active June 8, 2018 19:56
car, cdr, and push for pairlists in R
library(inline)
library(microbenchmark)
car <- cfunction(c(x = "pairlist"), '
if (x == R_NilValue)
return R_NilValue;
if (TYPEOF(x) != LISTSXP)
error("x must be a pairlist");
return CAR(x);
')
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this: