Skip to content

Instantly share code, notes, and snippets.

View jeroen's full-sized avatar

Jeroen Ooms jeroen

View GitHub Profile
@jeroen
jeroen / escape_chars.R
Last active August 29, 2015 14:06 — forked from wch/escape_chars.R
#' @useDynLib nameOfPackage C_escape_chars
deparse_vector3 <- function(x) {
.Call(C_escape_chars, x)
}
@jeroen
jeroen / json_http_stream.R
Last active August 29, 2015 14:06
Parse JSON HTTP stream
# This http data stream contains lines of minified JSON data. Therefore
# we can batch process this line by line. To speed things up, we actually
# process 100 lines at a time, by collapsing them into a JSON array.
library(jsonlite)
stopifnot(packageVersion("jsonlite") >= "0.9.11")
# note that open="r" results in line-by-line reading.
gzstream <- gzcon(url("http://78.46.48.103/sample/hourly_14.json.gz", open="r"))
batches <- list();
@jeroen
jeroen / rcurl_stream.R
Last active January 27, 2016 13:11
line-by-line import of json records with RCurl
batchimport <- function(url, batchsize = 1e6) {
# Globals
stack <- batches <- list();
i <- j <- 1;
size <- 0;
# Function that parses intermediate json
clearstack <- function(all=FALSE){
bigstr <- do.call(paste0, stack);
lines <- strsplit(bigstr, "\n")[[1]];
@jeroen
jeroen / mingw.sh
Last active March 15, 2023 12:04
mingw-w64 cross compile
# We use Ubuntu 14.04 to build a native gcc for win32 with multilib support
#
# Based on:
# http://sourceforge.net/p/mingw-w64/wiki2/Native%20Win64%20compiler/
# http://sourceforge.net/p/mingw-w64/code/HEAD/tree/stable/v3.x/mingw-w64-doc/howto-build/mingw-w64-howto-build.txt?format=raw
#
# Cross compiling notes:
# - The minor version of gcc must match that of our cross compiler (4.8 in this case)
# - Important parameters: http://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html
#
@jeroen
jeroen / silent-install.sh
Last active May 15, 2017 08:45
Silent install of OpenCPU
# Prevent postfix from prompting for hostname
sudo debconf-set-selections <<< "postfix postfix/mailname string your.hostname.com"
sudo debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
sudo apt-get install -y postfix
# Install OpenCPU
sudo add-apt-repository -y ppa:opencpu/opencpu-2.0
sudo apt-get update
sudo apt-get install -y opencpu-server
@jeroen
jeroen / curl.R
Last active August 29, 2015 14:08
curl() connection
# Synchronous implementation of curl() connection
curl <- function(url){
con <- rawConnection(raw(0), "r+");
cat("Downloading ")
req <- httr::GET(url, httr::write_stream(function(x){
cat(".")
writeBin(x, con)
}))
httr::stop_for_status(req)
@jeroen
jeroen / Rcpp-encoding.R
Last active August 29, 2015 14:11
Test encoding/recoding of strings in Rcpp
# UTF8 tests on Windows
# Using R 3.1.2 for win64 with LC_CTYPE=English_United States.1252
library(Rcpp)
# Part 1: simply echo a string
echo1src <- "#include <Rcpp.h>
// [[Rcpp::export]]
std::string echo1(std::string src){
@jeroen
jeroen / console.R
Last active August 29, 2015 14:11
Fun with V8 console
# Execute this script line by line in your favorite IDE
# It does not work via source()
library(V8)
data(diamonds, package="ggplot2")
# Create session
ct <- new_context()
ct$source("http://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js")
ct$assign("diamonds", diamonds)
@jeroen
jeroen / config.site
Last active April 9, 2020 02:59
Building R on Solaris
## Copy this file into config.site ##
CC="cc -xc99"
CPPFLAGS="-I/opt/csw/include"
CFLAGS="-I/opt/csw/include -O -xlibmieee"
LDFLAGS="-L/opt/csw/lib"
F77=f95
FFLAGS=-O
CXX="CC -library=stlport4"
CXXFLAGS=-O
FC=f95
@jeroen
jeroen / RJSONIO-errors.out
Last active August 29, 2015 14:13
Rcpp build error on Solaris Studio 12.3
> install.packages("RJSONIO")
Installing package into '/export/home/jeroen/R/i386-pc-solaris2.11-library/3.2'
(as 'lib' is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.rstudio.com/src/contrib/RJSONIO_1.3-0.tar.gz'
Content type 'application/x-gzip' length 1148375 bytes (1.1 MB)
opened URL
==================================================
downloaded 1.1 MB