Debian Codegraph @

inst/examples/knitr-beamer.Rnw

raw
1 %% LyX 2.2.1 created this file. For more info, see http://www.lyx.org/.
2 %% Do not edit unless you really know what you are doing.
3 \documentclass[10pt]{beamer}
4 \usepackage[T1]{fontenc}
5 \setcounter{secnumdepth}{3}
6 \setcounter{tocdepth}{3}
7 \usepackage{url}
8 \ifx\hypersetup\undefined
9 \AtBeginDocument{%
10 \hypersetup{unicode=true,pdfusetitle,
11 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
12 breaklinks=false,pdfborder={0 0 0},pdfborderstyle={},backref=false,colorlinks=false}
13 }
14 \else
15 \hypersetup{unicode=true,pdfusetitle,
16 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
17 breaklinks=false,pdfborder={0 0 0},pdfborderstyle={},backref=false,colorlinks=false}
18 \fi
19 \usepackage{breakurl}
20
21 \makeatletter
22
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
24 \providecommand{\LyX}{\texorpdfstring%
25 {L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\@}
26 {LyX}}
27
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
29 % this default might be overridden by plain title style
30 \newcommand\makebeamertitle{\frame{\maketitle}}%
31 % (ERT) argument for the TOC
32 \AtBeginDocument{%
33 \let\origtableofcontents=\tableofcontents
34 \def\tableofcontents{\@ifnextchar[{\origtableofcontents}{\gobbletableofcontents}}
35 \def\gobbletableofcontents#1{\origtableofcontents}
36 }
37
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
39 \usetheme{PaloAlto}
40
41 \makeatother
42
43 \begin{document}
44 <<setup, include=FALSE>>=
45 library(knitr)
46 opts_chunk$set(fig.path='figure/beamer-',fig.align='center',fig.show='hold',size='footnotesize')
47 @
48
49 \title[knitr, Beamer, and FragileFrame]{A Minimal Demo of knitr with Beamer and Fragile Frames}
50
51 \author{Yihui Xie\thanks{I thank Richard E. Goldberg for providing this demo.}}
52 \makebeamertitle
53 \begin{frame}{Background}
54
55 \begin{itemize}
56 \item The \textbf{knitr}\textbf{\emph{ }}package allows you to embed R code
57 and figures in \LaTeX{} documents
58
59 \begin{itemize}
60 \item It has functionality similar to Sweave but looks nicer and gives you
61 more control
62 \end{itemize}
63 \item If you already have Sweave working in \LyX{}, getting \textbf{knitr}
64 to work is trivial
65
66 \begin{enumerate}
67 \item Install the \textbf{knitr} package in \emph{R}
68 \item Read \url{https://yihui.org/knitr/demo/lyx/}
69 \end{enumerate}
70 \item If you use Sweave or \textbf{knitr} with Beamer in \LyX{}, you must
71 use the\emph{ FragileFrame} environment for the frames that contain
72 R code chunks. Let's see if \textbf{knitr} works with Beamer in this
73 small demo.
74 \end{itemize}
75 \end{frame}
76
77 \section{First Test}
78 \begin{frame}[fragile]{First Test}
79
80 OK, let's get started with just some text:
81
82 <<echo=FALSE,results='hide'>>=
83 # some setup
84 options(width=60) # make the printing fit on the page
85 set.seed(1121) # make the results repeatable
86 @
87
88 <<>>=
89 # create some random numbers
90 (x=rnorm(20))
91 mean(x);var(x)
92 @
93
94 BTW, the first element of \texttt{x} is \Sexpr{x[1]}. (Did you notice
95 the use of\texttt{ \textbackslash{}Sexpr\{\}}?)
96 \end{frame}
97
98 \section{Second Test}
99 \begin{frame}[fragile]{Second Test}
100
101 Text is nice but let's see what happens if we make a couple of plots
102 in our chunk:
103
104 <<boring-plots,fig.width=4,fig.height=4,out.width='.45\\linewidth'>>=
105 par(las=1,mar=c(4,4,.1,.1)) # tick labels direction
106 boxplot(x)
107 hist(x,main='',col="blue",probability=TRUE)
108 lines(density(x),col="red")
109 @
110 \end{frame}
111
112 \section{The Big Question}
113 \begin{frame}{The Big Question}
114
115 Do the above chunks work? You should be able to compile the \LyX{}
116 document and get a nice-looking PDF slide presentation. If not, time
117 to double-check everything...
118 \end{frame}
119
120 \end{document}