Teaching R with pgfSweave

For notes on how to use pgfSweave with LaTeX Beamer not related to teaching R, you might want to read Making Beamer and Sweave agree on size of tikz-generated graphs.

Here is a full template with good settings for creating a presentation with LaTeX Beamer and pgfSweave. To use it as is, create two directories for the cache:

mkdir -p sweave-cache/figs
mkdir -p sweave-cache/values

Now, you are ready to go, put the following in a file named myfilename.Rnw (or any filename, but it must match what is in \pgfrealjobname{} below AND must not include any extra dots except for the dot in ".Rnw").

\documentclass{beamer}

% LaTeX settings
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage{array}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{multirow}

% Sweave settings
\usepackage{tikz}
\pgfrealjobname{myfilename}
\usepackage[nogin]{Sweave}
\SweaveOpts{keep.source=TRUE,cache=F,prefix.string=sweave-cache/figs/fig}

\title{Here comes the title}
\author{Your Name}
\begin{document}
% Beamer settings
\definecolor{darkred}{rgb}{0.545,0,0}
\definecolor{midnightblue}{rgb}{0.098,0.098,0.439}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl,formatcom=\color{midnightblue}}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{formatcom=\color{darkred}}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl,formatcom=\color{blue}}

\maketitle

<<setup,echo=F,print=F,cache=F>>=
setCacheDir("sweave-cache/values")
options(width=60)
@

\section{Some header}

\subsection{Some problem 1}

\begin{frame}[fragile] \frametitle{3.1.a}
Calculate the probability for a standard normally distributed variable to be larger than 3.
\begin{small}
<<ISwR-3-1-0,fig=F,eval=F>>=
curve(dnorm, from = -4, to = 4)
abline(v=3)
abline(h=0)
@
\end{small}
% We do hide away some gory details as cex, and xlab
<<ISwR-3-1-1,fig=T,echo=F,width=3.375, height=2.7>>=
par(cex = 0.7)
curve(dnorm, from = -4, to = 4, xlab = "")
abline(v=3)
abline(h=0)
@
\end{frame}
\end{document}

To create the presentation, in R issue:

library(pgfSweave)
pgfSweave(file = "myfilename.Rnw", pdf = T)

This will give you a pdf-file with the following pages:

And page two:

(2011-01-20: Thanks to Johan Nylander for pointing out an error in the pgfSweave invocation-line above.)

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019