Kurs:Computer für Psychologen/Sitzung7

Aus Wikiversity

Links[Bearbeiten]

The Sweave Homepage


Folien[Bearbeiten]

Folien (tex-Source)[Bearbeiten]

<syntaxhighlight lang="latex"> \documentclass{beamer}

\usepackage[latin1]{inputenc} \usepackage[longnamesfirst]{natbib} \usepackage{listings}

\makeatletter  % work around for natbib \def\newblock{\beamer@newblock} \makeatother

\lstset{language=[LaTeX]TeX,%

 basicstyle=\ttfamily\scriptsize\color{blue!40!black},%
 frame=single,%
 commentstyle=\slshape\color{green!40!black},%

% keywordstyle=\bfseries\color{blue!50!black},% % identifierstyle=\color{blue!50!black},% % stringstyle=\color{red!90!black},%

 numbers=left,numberstyle=\tiny,%
 basewidth={.5em, .4em},%
 showstringspaces=false,%
 emphstyle=\color{red}

}

\beamertemplatenavigationsymbolsempty \setbeamertemplate{items}[circle] \setbeamercolor{box}{fg=white, bg=red} \setbeamercolor{button}{bg=white} \setbeamercolor{math text displayed}{fg=blue!80!black} \newcommand{\G}{\color{blue!40!black}}

%\setbeamertemplate{headline} %{ % \begin{beamercolorbox}{section in head} % \vskip5pt\insertnavigation{\paperwidth}\vskip5pt % \end{beamercolorbox} %}

\setbeamertemplate{footline}[frame number]

\useinnertheme{} \usecolortheme[named=purple]{structure} \usecolortheme{rose}

\title{{\bf Sweave -- Dynamic Interaction of R and \LaTeX{}

}}

\vspace{2cm} \author{Nora Umbach} \date{Dezember 2009}

\begin{document}

\thispagestyle{empty} \frame{\titlepage}

\begin{frame}{Why would I need Sweave?}

   \begin{itemize}
       \item Creating reports that can be updated automatically
       \item Statistic exercises
       \item Manuals with embedded examples (like R-help files, etc.)
       \item Avoiding copy/paste errors between analysis output and report
       file
       \item Reproducible research
   \end{itemize}
   \vfill

\end{frame}

\begin{frame}[fragile]{R package xtable -- create export tables}

   \begin{itemize}
       \item Function converting an R object to an `xtable' object, which
        can then be printed as a LaTeX or HTML table
       \item \begin{verbatim}xtable(x, caption=NULL, label=NULL,
       align=NULL, digits=NULL, display=NULL, ...)\end{verbatim}
       \item Check \verb+print.xtable()+ for useful arguments
       \item Example:
   \end{itemize}
   \lstset{language=R}
   \begin{lstlisting}
    1. Taken from help(lm) in R 1.1.1
    2. Annette Dobson (1990) "An Introduction to Generalized Linear Models".
    3. Page 9: Plant Weight Data.

ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) print(xtable(lm.D9)) print(xtable(anova(lm.D9)))

   \end{lstlisting}
   \vfill

\end{frame}

\begin{frame}[fragile]{R package xtable -- example}

   \begin{itemize}
       \item R output
   \end{itemize}
   \begin{lstlisting}

> print(xtable(anova(lm.D9))) % latex table generated in R 2.9.2 by xtable 1.5-6 package % Mon Dec 07 14:53:44 2009 \begin{table}[ht] \begin{center} \begin{tabular}{lrrrrr}

 \hline
& Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\ 
 \hline

group & 1 & 0.69 & 0.69 & 1.42 & 0.2490 \\

 Residuals & 18 & 8.73 & 0.48 &  &  \\ 
  \hline

\end{tabular} \end{center} \end{table}

   \end{lstlisting}

\end{frame}

\begin{frame}{R package xtable -- example}

   \begin{itemize}
       \item LaTeX output
   \end{itemize}
   % latex table generated in R 2.9.2 by xtable 1.5-6 package
   % Mon Dec 07 14:53:44 2009
   \begin{table}[ht]
   \begin{center}
   \begin{tabular}{lrrrrr}
     \hline
    & Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\ 
     \hline
   group & 1 & 0.69 & 0.69 & 1.42 & 0.2490 \\ 
     Residuals & 18 & 8.73 & 0.48 &  &  \\ 
      \hline
   \end{tabular}
   \end{center}
   \end{table}
   \vfill

\end{frame}

\begin{frame}[fragile]{Sweave example}

   \begin{lstlisting}

\documentclass[a4paper]{article}

\begin{document}

In this example we embed parts of the examples from the \texttt{kruskal.test} help page into a \LaTeX{} document:

<<>>= data(airquality) kruskal.test(Ozone ~ Month, data = airquality) @ which shows that the location parameter of the Ozone distribution varies significantly from month to month. Finally we include a boxplot of the data:

\begin{center} <<fig=TRUE,echo=FALSE>>= boxplot(Ozone ~ Month, data = airquality) @ \end{center}

\end{document}

   \end{lstlisting}

\end{frame}

\begin{frame}[fragile]{How to use Sweave}

   \begin{itemize}
       \item File will be saved as {\G \verb+filename.Snw+}
       \item Then run {\G \verb+Sweave("filename.Snw", stylepath=T)+}
       \item R generates {\G \verb+filename.tex+} and possible figures (do not
       forget to set working directory in R!)
       \item Now compile {\G \verb+filename.tex+}
       \item Done!
       \item If you want only the R code from your {\G
       \verb+filename.Snw+} use {\G \verb+Stangle("filename.Snw")+} and R
       will create {\G filename.R} with only R code from the R chunks
   \end{itemize}
   \vfill

\end{frame}

\begin{frame}[fragile]{Useful options} \begin{verbatim} <<>>= starts an R chunk @ starts a documentation chunk

       (ergo ends an R chunk)

\end{verbatim}

   \begin{itemize}
       \item options are defined by: `\verb+<<option>>=+' and seperated by
       commas
       \item {\G \verb+echo=false+} to hide R commands
       \item {\G \verb+results=hide+} when you do not want to have the
       results displayed
       \item {\G \verb+results=tex+} when output shall not be displayed as
       S output (for example when using {\G \verb+xtable()+})
       \item {\G \verb+fig=true+} when you want to insert a figure
   \end{itemize}
   \vfill

\end{frame}

\begin{frame}{Results of my thesis}

   \begin{itemize}
       \item Spatial cueing effects and awareness of target location:
       Evidence against early selection
       \item Subjects had to press a button when they saw a light (simple
       reaction time)
       \item With or without mirror, cue on right or left ear, light in
       right or left eye
   \end{itemize}
   \vfill

\end{frame}

\begin{frame}[fragile]{Documentation}

   On Friedrich Leisch's homepage you will find all the information you
   need about Sweave:
   {\color{purple}{
   \begin{verbatim}
   http://www.statistik.lmu.de/~leisch/Sweave/
   \end{verbatim}}}
   \nocite{Leisch2002}
   \nocite{Leisch2002b}
   \nocite{Leisch2003}
   \nocite{Leisch2003b}
   \vfill

\end{frame}

\begin{frame}[fragile]{Exercise} Write your own .Swn-file and compile it into a PDF:

   \begin{itemize}
       \item Load the dataset {\G \verb+cars+} and write a few short
       sentences about it (where can you find information on the dataset?)
       \item Create a table with descriptive statistics of the dataset
       (use {\G \verb+caption+} and {\G \verb+label+})
       \item Plot the data in a scatterplot and draw the regression line
       (use {\G \verb+lm()+}, {\G \verb+abline()+})
       \item Put the parameters of your regression model in a table in
       your report
       \item {\sc Hint:} for all tables use {\G \verb+xtable()+}, remember
       options: {\G \verb+echo=false+}, {\G \verb+results=hide+}, {\G
       \verb+results=tex+}, {\G \verb+fig=true+}
   \end{itemize}
   \vfill

\end{frame}

\begin{frame}{}

   \vfill
   \begin{center}\Huge {\color{purple}{\bf Thank you for your attention!}}
   \vfill
   \small nora.umbach@uni-tuebingen.de
   \end{center}

\end{frame}

\renewcommand\refname{} \begin{frame}{References}\small%[allowframebreaks]

   \bibliographystyle{newapa}
   \bibliography{U:/latex/nu}
   \vfill

\end{frame}

\end{document}