%\VignetteIndexEntry{Efficient R Programming}
%\VignettePackage{AdvancedR2011}

\documentclass{beamer}

\usepackage{BioconductorSlides}
\SweaveOpts{keep.source=TRUE, eval=TRUE}

\title{Efficient \R{} Programming}
\author{Martin Morgan}
\institute[FHCRC]{Fred Hutchinson Cancer Research Center}
\date{17-18 February, 2011}

\begin{document}

<<setup, echo=FALSE>>=
options(width = 40)
@ 

\frame{\titlepage}

\begin{frame}{Motivation}
  Challenges
  \begin{itemize}
  \item Long calculations: bootstrap, MCMC, \ldots.
  \item Big data: genome-wide association studies, re-sequencing,
    \ldots.
  \item Long $\times$ big: \ldots
  \end{itemize}
  Solutions
  \begin{itemize}
  \item Avoid \R{} programming pitfalls -- \emph{very} significant
    benefits
  \item Large data management
  \item Parallel evaluation, especially `embarrassingly parallel' (not
    discussed in this course)
  \end{itemize}
\end{frame}

\section{Programming pitfalls}

\subsection{Pitfalls and solutions}

\begin{frame}[fragile]{Programming pitfalls: easy solutions}
  \begin{itemize}
  \item Input only required data
<<io-sketch, eval=FALSE, keep.source=TRUE>>=
colClasses <-
  c("NULL", "integer", "numeric", "NULL")
df <- read.table("myfile", colClasses=colClasses)
@ 
%% 
  \item Preallocate-and-fill, not copy-and-append
<<preallocate-and-fill-sketch, eval=FALSE, keep.source=TRUE>>=
result <- numeric(nrow(df))
for (i in seq_len(nrow(df)))
  result[[i]] <- some_calc(df[i,])
@ 

  \item Vectorized calculations, not iteration
<<iterative, eval=TRUE, keep.source=TRUE>>=
x <- runif(100000); x2 <- x^2
m <- matrix(x2, nrow=1000); y <- rowSums(m)
@ 

  \item Avoid unnecessary character creation operations, e.g.,
    \Rfunarg{USE.NAMES=FALSE} in \Rfunction{sapply}, \Rfunarg{use.names=FALSE}
    in \Rfunction{unlist}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Programming pitfalls: moderate solutions}
  \begin{itemize}
  \item Use appropriate functions, often from specialized packages.
<<inappropriate-functions-2, eval=FALSE, keep.source=TRUE>>=
library(limma) # microarray linear models
fit <- lmFit(eSet, design)
@ 
  \item Identify appropriate algorithms, e.g., \Rfunction{\%in\%} is
    $O(N)$, whereas naive might be $O(N^2)$
<<algo-poly, keep.source=TRUE>>=
x <- 1:100; s <- sample(x, 10)
inS <- x %in% s
@ 
  \item Use C or Fortran code. Requires knowledge of other programming
    languages, and how to integrate these into \R{}
  \end{itemize}
\end{frame}

\subsection{Measuring performance}

\begin{frame}[fragile]{Measuring performance: timing}
  \begin{itemize}
  \item Use \Rfunction{system.time} to measure total evaluation time
    \begin{itemize}
    \item \Rfunarg{gcFirst=TRUE} for `garbage collection'
    \end{itemize}
  \item Use \Rfunction{replicate} to average over invocations
  \end{itemize}
<<system.time>>=
m <- matrix(runif(200000), 20000)
replicate(5, system.time(apply(m, 1, sum))[[1]])
replicate(5, system.time(rowSums(m))[[1]])
@ 
\begin{itemize}
\item Cautionary tale: \url{http://tinyurl.com/29bd6xv}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Measuring performance: comparison}
  \begin{itemize}
  \item \Rfunction{identical} and \Rfunction{all.equal} ensure that
    `optimizations' produce correct results!
  \end{itemize}
<<identical>>=
res1 <- apply(m, 1, sum)
res2 <- rowSums(m)
identical(res1, res2)
identical(c(1, -1), c(x=1, y=-1))
all.equal(c(1, -1), c(x=1, y=-1),
          check.attributes=FALSE)
@ 
\end{frame}

\begin{frame}[fragile, shrink]{Measuring execution time: \Rfunction{Rprof}}

<<Rprof, eval=FALSE, keep.source=TRUE>>=
tmpf = tempfile()
Rprof(tmpf)
res1 <- apply(m,  1, sum)
Rprof(NULL); summaryRprof(tmpf)
@ 
\begin{verbatim}
$by.self
         self.time self.pct total.time total.pct
"apply"       0.16       80       0.20       100
"FUN"         0.02       10       0.02        10
"lapply"      0.02       10       0.02        10
"unlist"      0.00        0       0.02        10

$by.total
         total.time total.pct self.time self.pct
"apply"        0.20       100      0.16       80
"FUN"          0.02        10      0.02       10
"lapply"       0.02        10      0.02       10
"unlist"       0.02        10      0.00        0
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Measuring memory use: \Rfunction{tracemem}}
  \begin{itemize}
  \item Enable memory profiling
  \end{itemize}
\begin{verbatim}
> ~/src/R-devel/configure --help
> ~/src/R-devel/configure --enable-memory-profiling
> make -j
\end{verbatim}
\begin{itemize}
\item Copy-on-change semantics
\end{itemize}
\begin{verbatim}
> x <- 1:10; tracemem(x)
[1] "<0x1b1a8f8>"
> y <- x       # no change, so no copy
> x[1] <- 2L   # x, y now differ, so copy
tracemem[0x1b1a8f8 -> 0x1b1a8a0]: 
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Measuring memory use: \Rfunction{tracemem}}
  \begin{itemize}
  \item Copying in \R{} functions
  \end{itemize}
\begin{verbatim}
> l <- list(a=1:10, b=1:10); tracemem(l$a)
[1] "<0x1131ce0>"
> df0 <- as.data.frame(l)
tracemem[0x1131ce0 -> 0x1131bd8]: eval as.data.frame.list as.data.frame 
tracemem[0x1131bd8 -> 0x1131a20]: data.frame eval eval as.data.frame.list as.data.frame 
tracemem[0x1131a20 -> 0x11318c0]: as.data.frame.integer as.data.frame data.frame eval eval as.data.frame.list as.data.frame 
> df1 <- data.frame(a=l$a, b=l$b)
tracemem[0x1131ce0 -> 0x11332c0]: data.frame 
tracemem[0x11332c0 -> 0x1133160]: as.data.frame.integer as.data.frame data.frame 
> identical(df0, df1)
[1] TRUE
\end{verbatim}
\end{frame}

\subsection{Debugging}

\begin{frame}[fragile]{Debugging: browsing and tracing}
  \begin{columns}
    \column{.5\textwidth}
      \begin{description}
      \item[\Rfunction{browser}] start the \emph{browser}
      \item[\Rfunction{debug}] enter browser when function invoked
      \item[\Rfunction{trace}] more flexible variant of
        \Rfunction{debug}
      \item[\Rfunction{setBreakpoint}] arbitrary action at particular
        line.
      \item[traceback] see the call stack at time of last error.
      \item[recover] select call stack location to invoke browser.
      \end{description}
      See Roger Peng's
      \href{http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf}{\emph{An
          Introduction to the Interactive Debugging Tools in \R{}}}
    \column{.5\textwidth}
\begin{semiverbatim}\only<1>{
> f <- function() \{
  x = 1
  browser() 
\}
> f()
Called from: f()
Browse[1]> ls()
[1] "x"
Browse[1]> x
[1] 1
Browse[1]> c
}\only<2>{
> f <- function(x) log(x)
> g <- function(i) f(i-1)
> trace(f, 
+ quote(if (x < 0) recover()))
> g(0)
Tracing f(i - 1) on entry 

Enter a frame number, 
   or 0 to exit   

1: g(0)
2: f(i - 1)

Selection: 1
Called from: eval.parent(exprObj)
Browse[1]> 
}
\end{semiverbatim}
  \end{columns}
\end{frame}

\begin{frame}[fragile]{Debugging: responding to errors}
  \begin{columns}
    \column{.4\textwidth}
    \Rfunction{options(\ldots)}
    \begin{itemize}
    \item \Rfunarg{warn=2} promote warnings to errors; reset with
      \Rfunarg{warn=0}
    \item \Rfunarg{error=recover} enter \Rfunction{recover} on error;
      reset with \Rfunarg{error=NULL}
    \end{itemize}
    \column{.6\textwidth}
\begin{semiverbatim}
> f <- function() warning("hmm")
> options(warn=2, error=recover)
> f()
Error in f() : 
  (converted from warning) hmm

Enter a frame number, or 0 to exit   

1: f()
2: warning("hmm")
\ldots
> options(warn=0, recover=NULL)
\end{semiverbatim}
  \end{columns}
\end{frame}

\section{Case Study: GWAS}

\begin{frame}[fragile]{Case study: GWAS}
  \begin{itemize}
  \item Subset of genome-wide association study data
  \item Manage sample and SNP annotations in SQL 
  \item Manage SNP genotypes in netCDF
  \item Coordinate access using S4 classes
  \item Perform calculalations (sliding window composite linkage
    disequilibrium) in C.
  \item Organize as \R{} package \Rpackage{StudentGWAS}
  \end{itemize}
\end{frame}

\end{document}

%% Local Variables:
%% compile-command: "R CMD make -f devmakefile EfficientR.pdf"
%% End:
