%\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}
