Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
@hadley
hadley / core-vs-members.r
Created August 12, 2014 18:53
Who's in R core vs. who's a member of the R foundation?
members <- c("Bill Venables", "Brian Ripley", "Douglas Bates", "Duncan Murdoch", "Duncan Temple Lang", "Friedrich Leisch", "John Chambers", "John Fox", "Kurt Hornik", "Luke Tierney", "Martin Maechler", "Martyn Plummer", "Paul Murrell", "Peter Dalgaard", "Robert Gentleman", "Roger Bivand", "Ross Ihaka", "Simon Urbanek", "Stefano Iacus", "Thomas Lumley")
core <- c("Douglas Bates", "John Chambers", "Peter Dalgaard", "Seth Falcon", "Robert Gentleman", "Kurt Hornik", "Stefano Iacus", "Ross Ihaka", "Friedrich Leisch", "Uwe Ligges", "Thomas Lumley", "Martin Maechler", "Duncan Murdoch", "Paul Murrell", "Martyn Plummer", "Brian Ripley", "Deepayan Sarkar", "Duncan Temple Lang", "Luke Tierney", "Simon Urbanek")
sort(setdiff(members, core))
sort(setdiff(core, members))
library(httr)
library(jsonlite)
find_endorsements <- function(x) {
r <- GET("https://www.linkedin.com/ta/skill" ,query = list(query = x))
stop_for_status(r)
json <- content(r, "parsed")
vapply(json$resultList, "[[", "displayName", FUN.VALUE = character(1))
library(httr)
library(XML)
library(selectr)
xpath <- function(x) structure(x, class = "xpath")
sel <- function(x) xpath(css_to_xpath(x, prefix = "//"))
url <- "http://www.boxofficemojo.com/movies/?id=ateam.htm"
html <- content(GET(url), "parsed")
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
RObject makeExplicit(SEXP prom) {
if (TYPEOF(prom) != PROMSXP) {
stop("Not a promise");
}
// recurse until we find the real promise, not a promise of a promise
#!/usr/bin/env Rscript
n <- as.integer(commandArgs(trailingOnly = TRUE))
lines <- readLines(file("stdin"))
words <- tolower(unlist(strsplit(lines, "\\W+")))
counts <- sort(table(words), decreasing = TRUE)
counts_n <- counts[1:n]
cat(sprintf("%8d %s\n", counts_n, names(counts_n)), sep = "")
png <- function(path, dpi = NULL) {
meta <- attr(png::readPNG(path, native = TRUE, info = TRUE), "info")
if (!is.null(dpi)) meta$dpi <- rep(dpi, 2)
meta$path <- path
structure(meta, class = "png")
}
knit_print.png <- function(x, options) {
str(options)
knitr::asis_output(paste0(
@hadley
hadley / rng-streams.R
Last active August 29, 2015 13:58
Manage multiple independent streams of random numbers
get_seed <- function() {
if (!exists(".Random.seed", envir = globalenv())) return(NULL)
get(".Random.seed", envir = globalenv())
}
set_seed <- function(x) {
old <- cur_seed()
if (!is.null(x)) {
assign(".Random.seed", x, envir = globalenv())
}
is_installed <- function(pkg, version = NULL) {
system.file(package = pkg) != "" && version_ok(pkg, version)
}
version_ok <- function(pkg, version = NULL) {
if (is.null(version)) return(TRUE)
pkgVersion(pkg) >= version
}
# This script uses httr to download data from Google's API
# Notice ther is a limit of 2,500 calls per day
library(httr)
base_url <- "http://maps.google.com/maps/api/geocode/json"
geoCode <- function(address,verbose=FALSE) {
r <- GET(base_url, query = list(address = address, sensor = "false"))
stop_for_status(r)