Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
@hadley
hadley / README.md
Created March 3, 2011 15:21
windows.r

Installing rggobi

The two scripts below give the most up-to-date instructions for installing rggobi on windows and mac. They should install everything you need, from Gtk2 to ggobi to rggobi.

To install:

  • On windows, copy and paste windows.r into R, and then run install_all().

  • On the mac, open the terminal, and copy and paste the commands from mac.sh below

a1 <- c(4.2,3.2,11.1,1.3,2.2,2.0)
a2 <- c(3.9,3.2,10.0,0.8,3.1,3.1)
a3 <- c(6.3,2.5,9.8,0.9,2.2,2.4)
a4 <- c(4.4,3.1,9.8,0.8,3.3,2.7)
a5 <- c(4.8,3.0,9.9,0.7,3.3,2.4)
a6 <- c(4.0,3.4,10.5,0.7,3.3,2.1)
a <- rbind(a1,a2,a3,a4,a5,a6)
# Get data into tidy format, starting with a, defined above
print.a <- function(x, ...) {
class(x) <- c("a", "c")
NextMethod()
}
print.b <- function(x, ...) cat("b\n")
print.c <- function(x, ...) cat("c\n")
ab <- structure(1:10, class = c("a", "b"))
ab
@hadley
hadley / gist:1545121
Created December 31, 2011 19:44
take.r
# Subsetting along (single) arbitrary dimension
take <- function(x, along, indices, drop = FALSE) {
nd <- length(dim(x))
index <- as.list(rep(TRUE, nd))
index[along] <- indices
eval(as.call(c(as.name("["), as.name("x"), index, drop = drop)))
}
a <- function() 10
f <- function() {
a <- 5
g <- function() {
print(a)
print(a())
}
g
}
@hadley
hadley / profile.R
Created March 27, 2012 20:13
my .Rprofile
.First <- function() {
library(grDevices)
options(
repos = c(CRAN = "http://cran.r-project.org/"),
browserNLdisabled = TRUE,
deparse.max.lines = 2,
device = "quartz")
}
if (interactive()) {
f <- function() {
c <- match.call()
c[[1]] <- as.name("get")
c[[2]] <- "get"
c
as.function(c(alist(), c))
}
f()()("x")
f <- function() {
# Inspired by Andreas Buja's code from
# http://stat.wharton.upenn.edu/~buja/STAT-101/src-probability.R
rv <- function(x, probs = NULL) {
if (is.null(probs)) {
probs <- rep(1, length(x)) / length(x)
}
vals <- as.numeric(x)
ord <- order(vals)
library(tools)
show_rd2 <- function(pkg = NULL, file, ...) {
pkg <- as.package(pkg)
if (file_ext(file) == "") file <- paste(file, ".Rd", sep ="")
path <- file.path(pkg$path, "man", file)
stopifnot(file.exists(path))
temp <- Rd2HTML(path, out = tempfile("Rtxt"), package = pkg$package,
...)
library(httr)
library(plyr)
# stackexchange(ids = 16632)
stackexchange <- function(page = NULL, pagesize = NULL, fromdate = NULL, todate = NULL, order = NULL, min = NULL, max = NULL, sort = NULL, ids,
url = "https://api.stackexchange.com/2.0/users/")
{
query <- compact(list(page = page, pagesize = pagesize, fromdate = fromdate,
todate = todate, order = order, min = min, max = max, sort = sort,
site = "stackoverflow"))