Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
@hadley
hadley / loop.R
Last active November 10, 2022 20:11
f1 <- function(n) {
x <- numeric()
for (i in 1:n) {
x <- c(x, i)
}
x
}
f1.5 <- function(n) {
x <- numeric()
@hadley
hadley / rstats-spam.R
Last active September 26, 2022 12:43
library(rtweet)
library(tidyverse)
auth_setup_default()
json <- search_tweets("#rstats", n = 5000, include_rts = FALSE, parse = FALSE)[1:5]
tweets <- tibble(json = json) %>%
unnest_wider(json) %>%
select(statuses) %>%
unnest_longer(statuses) %>%
#!/bin/bash
#
# Installs the latest RStudio daily desktop build for OSX/macOS and Ubuntu(amd64)
#
# https://support.rstudio.com/hc/en-us/articles/203842428-Getting-the-newest-RStudio-builds
set -e
install_macos_daily() {
REDIRECT_URL="https://www.rstudio.org/download/latest/daily/desktop/mac/RStudio-latest.dmg"
echo "Discovering daily build from: ${REDIRECT_URL}"
# Perform a HEAD request to find the redirect target. We use the name of the
@hadley
hadley / r-is-like.md
Last active December 13, 2021 22:41
A lighthearted and overly flattering comparison of R to other programming languages: what other comparisons have I missed?

R is like:

  • Clojure, because most objects are immutable
  • Scala, because it combined functional and OO techniques
  • Node.js, because the interpreter is single threaded
  • PHP, because it favours pragmatism over purity
  • Lisp, because it's homoiconic
  • Perl, because OO is (mostly) implemented using the language itself
# lazy data frame
#
# like idata.frame, but exploits lazy evaluation + macro code generation
# instead of active bindings
library(vadr)
lazy.frame <- function(df, enclos=parent.frame(), ...) UseMethod("lazy.frame")
lazy.frame.lazy.frame <- function(df, ...) df
# Code for quick exploration of
# https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-05-26
# Video at https://youtu.be/kHFmtKCI_F4
library(tidyverse)
cocktails <- readr::read_csv("boston_cocktails.csv")
# Are name and row_id equivalent? -----------------------------------------
library(memoise)
library(stringr)
git_prompt <- function() {
git <- find_git()
if (is.null(git)) stop("Git not installed", call. = FALSE)
if (!in_git_repo(git)) stop("Not in git repo", call. = FALSE)
update <- function(...) update_prompt(git)
@hadley
hadley / handlers.md
Created May 31, 2018 14:59
Walk through of R's condition handler C code

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.
ks.default <- function(rows) seq(2, max(3, rows %/% 4))
many_kmeans <- function(x, ks = ks.default(nrow(x)), ...) {
ldply(seq_along(ks), function(i) {
cl <- kmeans(x, centers = ks[i], ...)
data.frame(obs = seq_len(nrow(x)), i = i, k = ks[i], cluster = cl$cluster)
})
}
all_hclust <- function(x, ks = ks.default(nrow(x)), point.dist = "euclidean", cluster.dist = "ward") {
@hadley
hadley / curriculum.md
Created September 27, 2013 20:24
My first stab at a basic R programming curriculum. I think teaching just these topics without overall motivating examples would be extremely boring, but if you're a self-taught R user, this might be useful to help spot your gaps.

Notes:

  • I've tried to break up in to separate pieces, but it's not always possible: e.g. knowledge of data structures and subsetting are tidy intertwined.

  • Level of Bloom's taxonomy listed in square brackets, e.g. http://bit.ly/15gqPEx. Few categories currently assess components higher in the taxonomy.

Programming R curriculum

Data structures