Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
library(rlang)
enum_value <- function(x, values) {
structure(
x,
values = values,
)
}
enum <- function(...) {
library(tidyverse)
library(rvest)
url <- "https://en.wikipedia.org/wiki/The_Great_British_Bake_Off_(series_3)"
page <- read_html(url)
table <- page %>%
html_nodes("table.wikitable") %>%
.[[2]]
@hadley
hadley / devtools.r
Last active April 16, 2019 21:13
Install the development version of devtools on windows.
if ("devtools" %in% loadedNamespaces()) {
stop("You must restart R before installing devtools")
}
url <- "https://gist.github.com/raw/4506250/devtools.zip"
temp <- file.path(tempdir(), "devtools.zip")
setInternet2(TRUE)
suppressWarnings(download.file(url, temp, mode = "wb"))
install.packages(temp, repos = NULL)
ruler <- function(width = getOption("width")) {
x <- seq_len(width)
y <- dplyr::case_when(
x %% 10 == 0 ~ as.character((x %/% 10) %% 10),
x %% 5 == 0 ~ "+",
TRUE ~ "-"
)
cat(y, "\n", sep = "")
cat(x %% 10, "\n", sep = "")
}
@hadley
hadley / a4.txt
Created August 9, 2018 15:29
The first R script I can find on my computer
library(class)
library(mass)
library(mva)
tst <- read.table("e:/uni/stats766/puktest.txt", header = TRUE)
tst.v <- tst[,1:7]
tst.g <- tst[,8]
trn <- read.table("e:/uni/stats766/puktrain.txt", header = TRUE)
trn.v <- trn[,1:7]
trn.g <- trn[,8]
library(tidyverse)
library(lubridate)
url1 <- "https://cran.rstudio.com/src/contrib/Meta/archive.rds"
url2 <- "https://cran.rstudio.com/src/contrib/Meta/current.rds"
if (!file.exists(basename(url1))) download.file(url1, basename(url1), quiet = TRUE)
if (!file.exists(basename(url2))) download.file(url2, basename(url2), quiet = TRUE)
archive <- readRDS("archive.rds")
@hadley
hadley / latex-math.r
Created May 14, 2013 14:18
A partial implementation of a R expression -> latex math converter
# User facing function
#
# to_math(x_1 + 1^{2 + 4} + 5 + sqrt(y) / 5 %/% 10)
# to_math(paste(x^2, y - 1, z_i))
# to_math(hat(tilde(ring(x))))
to_math <- function(x) {
to_math_q(substitute(x))
}
to_math_q <- function(x) {
# Constructor and basic methods ---------------------------------------------
new_rational <- function(n, d) {
stopifnot(is.integer(n), is.integer(d))
stopifnot(length(n) == length(d))
structure(list(d = d, n = n), class = "rational")
}
length.rational <- function(x) {

Registering handlers

The key C function that powers both tryCatch() and withCallingHandlers() is do_addCondHands(). It creates handler object with mkHandlerEntry() then stores in the handler stack for the current frame. (More precisely it writes to R_HandlerStack, a global variable that is an alias to c->handlerstack)

The five R arguments to do_addCondHands() are classes, handlers, parentenv, target, and calling. These are combined with a result object (a list of length 4, returned by the exiting handler to doTryCatch()) to create the handler objects which have five components:

  • The class, accessed with ENTRY_CLASS(e). A string given a class name; the handler will match all conditions that contain this component in their class vector.
@hadley
hadley / frame.R
Last active October 22, 2017 18:28
frame <- function(from = -Inf, to = 0) {
if (from >= to) stop("from must be less than to", call. = FALSE)
dir <- function(x) if (x < 0) "PRECEDING" else "FOLLOWING"
val <- function(x) if (is.finite(x)) as.integer(x) else "UNBOUNDED"
bound <- function(x) {
if (x == 0) return("CURRENT ROW")
paste(val(x), dir(x))
}