1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Simple JSON RPC with OpenCPU}
%This is a template.
%Actual text goes in sources/content.Rnw
\documentclass{article}
\author{Jeroen Ooms}
%useful packages
\usepackage{url}
\usepackage{fullpage}
\usepackage{xspace}
\usepackage{hyperref}
\usepackage{fancyvrb}
%for table positioning
\usepackage{float}
\restylefloat{table}
%support for accents
\usepackage[utf8]{inputenc}
%support for ascii art
\usepackage{pmboxdraw}
%use vspace instead of indentation for paragraphs
\usepackage{parskip}
%extra line spacing
\usepackage{setspace}
\setstretch{1.25}
%knitr style verbatim blocks
\newenvironment{codeblock}{
\VerbatimEnvironment
\definecolor{shadecolor}{rgb}{0.95, 0.95, 0.95}\color{fgcolor}
\color{black}
\begin{kframe}
\begin{BVerbatim}
}{
\end{BVerbatim}
\end{kframe}
}
%placeholders for JSS/RJournal
\newcommand{\pkg}[1]{\texttt{#1}}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\file}[1]{\texttt{#1}}
\newcommand{\dfn}[1]{\emph{#1}}
\newcommand{\proglang}[1]{\texttt{#1}}
%shorthands
\newcommand{\JSON}{\texttt{JSON}\xspace}
\newcommand{\R}{\texttt{R}\xspace}
\newcommand{\C}{\texttt{C}\xspace}
\newcommand{\toJSON}{\texttt{toJSON}\xspace}
\newcommand{\fromJSON}{\texttt{fromJSON}\xspace}
\newcommand{\XML}{\pkg{XML}\xspace}
\newcommand{\jsonlite}{\pkg{jsonlite}\xspace}
\newcommand{\RJSONIO}{\pkg{RJSONIO}\xspace}
\newcommand{\API}{\texttt{API}\xspace}
\newcommand{\JavaScript}{\texttt{JavaScript}\xspace}
%trick for using same content file as chatper and article
\newcommand{\maintitle}[1]{
\title{#1}
\maketitle
}
%actual document
\begin{document}
\section*{Simple \JSON RPC with OpenCPU}
The \jsonlite package is used by \texttt{OpenCPU} to convert between \JSON data and \R objects. Thereby clients can retrieve \R objects, or remotely call \R functions using \JSON where the function arguments as well as function return value are \JSON objects. For example to download the \texttt{Boston} data from the \texttt{MASS} package:\\
\begin{tabular}{|l|l|}
\hline
\textbf{Command in R} & \textbf{Example URL on OpenCPU} \\
\hline
\texttt{toJSON(Boston, digits=4)} & \url{https://cran.ocpu.io/MASS/data/Boston/json?digits=4} \\
\hline
\texttt{toJSON(Boston, dataframe="col")} & \url{https://cran.ocpu.io/MASS/data/Boston/json?dataframe=col} \\
\hline
\texttt{toJSON(Boston, pretty=FALSE)} & \url{https://cran.ocpu.io/MASS/data/Boston/json?pretty=false} \\
\hline
\end{tabular}
\newline
To calculate the variance of some the numbers \texttt{1:9} in the command line using using \texttt{curl}:
\begin{Verbatim}[frame=single]
curl https://cran.ocpu.io/stats/R/var/json -d "x=[1,2,3,4,5,6,7,8,9]"
\end{Verbatim}
Or equivalently post the entire body in \JSON format:
\begin{Verbatim}[frame=single]
curl https://cran.ocpu.io/stats/R/var/json -H "Content-Type: application/json" \
-d "{\"x\":[1,2,3,4,5,6,7,8,9]}"
\end{Verbatim}
Below an example where we call the \texttt{melt} function from the \texttt{reshape2} package using some example rows from the \texttt{airquality} data. Here both input and output consist of a data frame.
\begin{Verbatim}[frame=single]
curl https://cran.ocpu.io/reshape2/R/melt/json -d 'id=["Month", "Day"]&data=[
{ "Ozone" : 41, "Solar.R" : 190, "Wind" : 7.4, "Temp" : 67, "Month" : 5, "Day" : 1 },
{ "Ozone" : 36, "Solar.R" : 118, "Wind" : 8, "Temp" : 72, "Month" : 5, "Day" : 2 } ]'
\end{Verbatim}
Or equivalently:
\begin{Verbatim}[frame=single]
curl https://cran.ocpu.io/reshape2/R/melt/json -H "Content-Type: application/json" \
-d '{"id" : ["Month", "Day"], "data" : [
{ "Ozone" : 41, "Solar.R" : 190, "Wind" : 7.4, "Temp" : 67, "Month" : 5, "Day" : 1 },
{ "Ozone" : 36, "Solar.R" : 118, "Wind" : 8, "Temp" : 72, "Month" : 5, "Day" : 2 }
] }'
\end{Verbatim}
This request basically executes the following \R code:
<<eval=FALSE>>=
mydata <- airquality[1:2,]
y <- reshape2::melt(data = mydata, id = c("Month", "Day"))
toJSON(y)
@
%end
\end{document}
|