Skip to content

Instantly share code, notes, and snippets.

Hadley Wickham hadley

View GitHub Profile
View rational.R
# 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) {
View ruler.R
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 Aug 9, 2018
The first R script I can find on my computer
View a4.txt
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]
@hadley
hadley / handlers.md
Created May 31, 2018
Walk through of R's condition handler C code
View handlers.md

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.
View handlers.md

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.
View craniversary.R
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 / stuff.md
Created Aug 14, 2017
gist gist gist
View stuff.md

Stuff and things

head of mtcars

head(mtcars)
@hadley
hadley / stuff.md
Created Aug 14, 2017
gist gist gist
View stuff.md

Stuff and things

head of mtcars

head(mtcars)
@hadley
hadley / stuff.md
Created Aug 8, 2017
gist gist gist
View stuff.md

Stuff and things

head of mtcars

head(mtcars)
@hadley
hadley / stuff.md
Created Aug 8, 2017
gist gist gist
View stuff.md

Stuff and things

head of mtcars

head(mtcars)
You can’t perform that action at this time.