Skip to content

Instantly share code, notes, and snippets.

View jeroen's full-sized avatar

Jeroen Ooms jeroen

View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<title>OpenCPU demo app</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- ocpu library -->
<script src="//code.jquery.com/jquery-1.10.2.min.js"> </script>
<script src="//public.opencpu.org/js/opencpu-0.4.js"> </script>
#Assume httr 0.4
library(httr)
stopifnot(packageVersion("httr") >= "0.4")
#submit a package to CRAN, step 1
submit <- function(name, email, pkg, comment = "Thank you", popup = TRUE){
stopifnot(file.exists(pkg))
req1 = httr::POST (
url = "http://xmpalantir.wu.ac.at/cransubmit/index2.php",
encode = "multipart",
@jeroen
jeroen / weather.R
Last active August 29, 2015 14:05
Batch parsing JSON streams with jsonlite
# The data stream is not JSON itself. It contains lines with JSON data. Therefore
# this example manually collapses it into a JSON array. The file is also quite
# large, so we would need to do some batch processing by splitting the file before
# feeding it to R. This example shows how to read first 20 records.
library(jsonlite)
gzdata <- gzcon(url("http://78.46.48.103/sample/hourly_14.json.gz"))
records <- readLines(gzdata, n = 20)
close(gzdata)
json <- paste0("[", paste0(records, collapse=","), "]")
@jeroen
jeroen / deparse_vector.R
Created September 1, 2014 17:00
Performance comparison of deparsing character vectors
# Version 1
deparse_vector1 <- function(x) {
stopifnot(is.character(x))
vapply(x, deparse, character(1), USE.NAMES=FALSE)
}
# Version 2
deparse_vector2 <- function(x) {
stopifnot(is.character(x))
if(!length(x)) return(x)
@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 / 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 / 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