forked from yihui/knitr-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
005-latex.Rtex
54 lines (44 loc) · 1.48 KB
/
005-latex.Rtex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
\documentclass{article}
\usepackage{graphicx}
%% for inline R code: if the inline code is not correctly parsed, you will see a message
\newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
%% begin.rcode setup, include=FALSE
% library(knitr)
% opts_chunk$set(fig.path='figure/latex-', cache.path='cache/latex-')
%% end.rcode
\begin{document}
Boring stuff as usual:
%% a chunk with default options
%% begin.rcode
% 1+1
%
% x=rnorm(5)
%% end.rcode
For the cached chunk below, you will need to wait for 10 seconds for
the first time you compile this document, but it takes no time the
next time you run it again.
%% chunk options: cache this chunk
%% begin.rcode my-cache, cache=TRUE
% set.seed(123)
% x = runif(10)
% sd(x) # standard deviation
%
% Sys.sleep(10) # test cache
%% end.rcode
Now we know the first element of x is \rinline{x[1]}. And we also know
the 26 letters are \rinline{LETTERS}. An expression that returns a
value of length 0 will be removed from the output, \rinline{x[1] =
2011; NULL} but it was indeed evaluated, e.g. now the first element
of x becomes \rinline{x[1]}.
How about figures? Let's use the Cairo PDF device (assumes R $\geq$
2.14.0).
%% begin.rcode cairo-scatter, dev='cairo_pdf', fig.width=5, fig.height=5, out.width='.8\\textwidth'
% plot(cars) # a scatter plot
%% end.rcode
Warnings, messages and errors are preserved by default.
%% begin.rcode
% sqrt(-1) # here is a warning!
% message('this is a message you should know')
% 1+'a' # impossible
%% end.rcode
\end{document}