Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
# Draw R expressions as ascii trees
library(stringr)
library(testthat) # for the colourise function
str_trunc <- function(x, width = getOption("width")) {
ifelse(str_length(x) <= width, x, str_c(str_sub(x, 1, width - 3), "..."))
}
is.constant <- function(x) !is.language(x)
is.leaf <- function(x) {
<!DOCTYPE html>
<meta charset="utf-8">
<style></style>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
<script src="http://coffeescript.org/extras/coffee-script.js"></script>
<script src="table.coffee" type="text/coffeescript"></script>
<!DOCTYPE html>
<meta charset="utf-8">
<style></style>
<body>
<script src="http://d3js.org/d3.v2.js?2.9.6"></script>
<script src="http://coffeescript.org/extras/coffee-script.js"></script>
<script src="vis.coffee" type="text/coffeescript"></script>
<svg width="400" height="400" id="chart"></svg>
with(x, {
f(z)
})
# Which of the following statements is this equivalent to?
#
# a) x$f(x$z)
# b) f(x$z)
# c) x$f(z)
# d) f(z)
library(tm)
x <- paste(sample(c(LETTERS, " "), 1e4, rep = T), collapse = "")
words <- stopwords("english")
match <- sprintf("\\b(%s)\\b", paste(words, collapse = "|"))
system.time(gsub(match, "", x))
# Takes ~0.08 s
# Home made solution, not quite equivalent but close.
library(stringr)
library(tm)
if (file.exists("english.rds")) {
data <- readRDS("english.rds")
} else {
data <- read.csv("english_text.csv.gz", stringsAsFactors = FALSE)
saveRDS(data, "english.rds")
}
#include <R.h>
#include <Rdefines.h>
#ifdef WIN32
#include <shlobj.h>
// SHGetFolderPath documentation:
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181.aspx
SEXP get_folder_path() {
vertoccurrence <- function(key="r_B68F3", grp="fish", t = NULL, l = NULL,
c = NULL, d = NULL, q = NULL, p = NULL, m = NULL, cols = NULL, num = NULL,
set = NULL)
{
grp <- match.arg(grp, c("bird", "herp", "fish"))
url <- c(
bird = "http://ornis2.ornisnet.org/api/v1/occurrence/?",
herp = "http://herpnet2.org/api/v1/occurrence/?",
fish = "http://www.fishnet2.net/api/v1/occurrence/?"
)[grp]
# OR with httr
library(httr)
vertoccurrence <- function(key="r_B68F3", grp="fish", t = NULL, l = NULL,
c = NULL, d = NULL, q = NULL, p = NULL, m = NULL, cols = NULL, num = NULL,
set = NULL)
{
grp <- match.arg(grp, c("bird", "herp", "fish"))
url <- c(
bird = "http://ornis2.ornisnet.org/api/v1/occurrence/",
document <- function(pkg = NULL, clean = FALSE, roclets = c("collate", "namespace", "rd")) {
require("roxygen2")
man_path <- file.path(pkg$path)
if (!file.path(pkg$path)) dir.create(man_path)
message("Updating ", pkg$package, " documentation")
pkg <- as.package(pkg)
if (clean) {
clear_caches()