% Created 2011-07-30 Sat 15:42
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\providecommand{\alert}[1]{\textbf{#1}}

\title{Some simple R code blocks in an org buffer.}
\author{Stephen Eglen}
\date{\today}

\begin{document}

\maketitle

\setcounter{tocdepth}{3}
\tableofcontents
\vspace*{1cm}

\section{Introduction}
\label{sec-1}


  This is an example document to demonstrate some simple R blocks in
  an org document.  Convert it either to pdf or html using the
  appropriate export tools.
\subsection{Getting started}
\label{sec-1-1}


   Here we compare the output from \texttt{:results value} with \texttt{:results    output}.  Within each code block, press \texttt{C-c C-c} to see (or update)
   the results.  


\begin{verbatim}
1 + 2
3 + 4
\end{verbatim}




\begin{verbatim}
    7
\end{verbatim}




\begin{verbatim}
1 + 2
3 + 4
\end{verbatim}




\begin{verbatim}
    [1] 3
    [1] 7
\end{verbatim}
\section{Process an org table in R}
\label{sec-2}


  We use org-mode to create a nice-looking table, which we want to
  process in R.


\begin{center}
\begin{tabular}{lrr}
 name   &  maths  &  physics  \\
\hline
 joe    &     75  &       90  \\
 fred   &     80  &       50  \\
 harry  &     90  &       75  \\
\end{tabular}
\end{center}
\subsection{Process the results}
\label{sec-2-1}


   We would like to read in the table, and then compute the weighted
   sum of the scores for each student.  Note how we refer to the table
   above through its \emph{tblname}.


\begin{verbatim}
ave <- (0.7 * scores$maths) + (0.3 * scores$physics)
data.frame(name=scores$name, score=ave)
\end{verbatim}




\begin{verbatim}
       name score
    1   joe  79.5
    2  fred  71.0
    3 harry  85.5
\end{verbatim}
\section{Comparing base vs lattice graphics output}
\label{sec-3}


  Unfortunately you need to have slightly different expressions for
  generating \emph{base} and \emph{lattice} graphics output.
  
\subsection{Example of base graphics output}
\label{sec-3-1}


   With base graphics, e.g. to make a histogram, try the following
   snippet.  Here we generate a png file, just hit \texttt{C-c C-c} within
   the code to eval it, and generate the resulting file.


\begin{verbatim}
hist( rnorm(1e4))
\end{verbatim}




   \includegraphics[width=.9\linewidth]{hist.png}


   Emacs can view png's within a buffer, and even show them inline
   using the minor mode \texttt{iimage-mode}.  Turn this on with \texttt{M-x    turn-on-image-mode}.  (Warning: this sometimes places the images in
   the wrong place.)
\subsection{Example of lattice graphics output}
\label{sec-3-2}


   When using lattice graphics, you will need to set \texttt{:results output    graphics} rather than just \texttt{:results graphics}.  For example, here
   we generate a contour plot of the function

   \[ \log F - F + \log S - S = k \]

   where F and S are varied to see what the constant k should be.
   Note that we export the code, rather than exporting both the code
   and the results (in this case the graphics).  The results line is
   still output though (delete it to check).  However, note that the
   caption line must come before the file: line.


\begin{verbatim}
library(lattice)
f <- seq(from=0.1, to=4, length=100)
s <- f
grid <- expand.grid(x=f, y=s)
grid$z <- log(grid$x) - grid$x + log(grid$y) - grid$y
levelplot(z ~ x + y, grid, more=TRUE, contour=TRUE,xlab='fish', ylab='shark')
\end{verbatim}




   \begin{figure}[htb]
   \centering
   \includegraphics[width=.9\linewidth]{fishlevels.png}
   \caption{Lattice contour plot showing populations of sharks and fish.}
   \end{figure}
\section{Passing results from one language to another via org.}
\label{sec-4}


  We can have code snippets in different languages, and plug them
  together using the textual output from one code block.  Here is a
  simple example where we take some output from a shell command and
  feed it into an R block.  (Of course, this example can be done
  entirely in R \ldots{})



\begin{verbatim}
ls -sS /etc | grep -v total | head -5
\end{verbatim}





\begin{center}
\begin{tabular}{rl}
 376  &  services         \\
  16  &  moduli           \\
  48  &  php.ini.default  \\
  32  &  amavisd.conf     \\
  64  &  authorization    \\
\end{tabular}
\end{center}






  This table is named \emph{biggest-files}, which can be passed to an R function to create
  a little pie chart:


\begin{verbatim}
pie(f[,1], labels = f[,2])
\end{verbatim}




  \includegraphics[width=.9\linewidth]{pie.png}
\section{Why :session?}
\label{sec-5}


  We have not used :session here in the calls, for simplicity.  But you
  might find it useful, for several reasons.

\begin{itemize}
\item You can see what is happening, rather than running in batch.
\item A bit quicker than starting a new session each time.
\item State kept from one block to another in the process.
\end{itemize}

  Example from \href{http://orgmode.org/worg/org-contrib/babel/intro.html}{http://orgmode.org/worg/org-contrib/babel/intro.html}
\section{Sources}
\label{sec-6}


  \href{http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html}{http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html}

  The main documentation page.  Many examples adapted from here or other
  Org Babel (OB) pages.

\end{document}