Skip to content

Instantly share code, notes, and snippets.

View jeroen's full-sized avatar

Jeroen Ooms jeroen

View GitHub Profile
@jeroen
jeroen / closures.R
Last active August 29, 2015 14:18
Closures in R
# Creating closures in R is actually very fast:
data("diamonds", package = "ggplot2")
diamonds$get <- lapply(seq_len(nrow(diamonds)), function(i){
k <- i
function(what){
diamonds[k,what]
}
})
# Execute:
@jeroen
jeroen / readme.md
Last active January 17, 2017 17:21
Testing R packages on Solaris
@jeroen
jeroen / abstract2015.md
Created April 11, 2015 22:07
Abstract for 2015 R talk(s)

jsonlite and mongolite

The jsonlite package provides a powerful JSON parser and generator that has become one of standard methods for getting data in and out of R. We discuss some recent additions to the package, in particular support streaming (large) data over http(s) connections. We then introduce the new mongolite package: a high-performance MongoDB client based on jsonlite. MongoDB (from "humongous") is a popular open-source document database for storing and manipulating very big JSON structures. It includes a JSON query language and an embedded V8 engine

@jeroen
jeroen / dat.R
Last active August 29, 2015 14:20
dat in R
# If you are running beta version, you might need to symlink 'dat-beta' to 'dat'
# Dependencies
library(jsonlite)
data("diamonds", package = "ggplot2")
# Initiate the dat repo
setwd(tempdir())
system2("dat", "init")
@jeroen
jeroen / diff.R
Last active August 29, 2015 14:20
# Dependencies
library(jsonlite)
# Initiate the dat repo
setwd(tempdir())
system2("dat", "init")
# Add 10 lines of data
stream_out(iris[1:10,], pipe("dat add -"))
head1 <- readLines(pipe("dat heads"))
@jeroen
jeroen / dat.md
Last active August 29, 2015 14:21
dat API proposal

Most intuitive to me would be if dat import reads jsonlines or csv:

curl https://jeroenooms.github.io/data/diamonds.json | dat import -d diamonds
curl https://demo.ocpu.io/MASS/data/cats/csv | dat import -d cats --csv
cat mydata.csv | dat import -d mydata --csv

And dat export does the opposite: stream out jsonlines/csv to be used in a pipe:

@jeroen
jeroen / k-means-mongo.R
Last active August 29, 2015 14:21
In-database k-means using MongoDB mapreduce
# Use MongoDB map-reduce for in-database k-means clustering
# Requires recent version of monoglite:
# devtools::install_github("jeroenooms/mongolite")
stopifnot(packageVersion("mongolite") >= "0.3.9001")
library(mongolite)
# Connect to 'diamonds' collection
m <- mongo("diamonds")
# Wipe old data if any
@jeroen
jeroen / .profile
Last active August 29, 2015 14:22
Solaris .profile
# Profile only works after changing default shell to bash with: passwd -e
# If you are root just store in /.profile
# Set Terminal
export TERM=xterm
# To find solaris 'make'
export PATH=$PATH:/usr/ccs/bin:/usr/sfw/bin
# To find opencsw libraries
@jeroen
jeroen / test.c
Last active August 29, 2015 14:22
Authentication bug in libcurl
/* Compile with:
* cc -lcurl test.c -o test
*/
#include <curl/curl.h>
int main (int argc, char *argv[]) {
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *handle = curl_easy_init();
//comment out to fix problem
@jeroen
jeroen / cran.R
Last active August 29, 2015 14:23
Accessing CRAN via https
# Script to list CRAN servers with https
library(curl)
h <- new_handle(timeout_ms = 30000, connecttimeout_ms = 5000)
mirrors <- read.csv(curl("https://svn.r-project.org/R/trunk/doc/CRAN_mirrors.csv"))
mirrors$SSL <- vapply(mirrors$URL, function(url){
https_url <- paste0(sub("^http://", "https://", url), "src/contrib/PACKAGES")
cat("Trying", https_url, "\n")
identical(200L, try(curl_fetch_memory(https_url, handle = h)$status))
}, logical(1))
subset(mirrors, SSL == TRUE, select = c("Name","URL"))