Sharedlatex-tests / rtex.RtexOpen in CoCalc
Author: Harald Schilly
1
\documentclass{article}
2
\usepackage[utf8]{inputenc}
3
\usepackage[T1]{fontenc}
4
\usepackage{url}
5
\usepackage{graphicx}
6
7
% this is based on https://github.com/yihui/knitr-examples/blob/master/005-latex.Rtex
8
9
%% for inline R code: if the inline code is not correctly parsed, you will see a message
10
\newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
11
12
%% begin.rcode setup, include=FALSE
13
% library(knitr)
14
% opts_chunk$set(fig.path='figure/latex-', cache.path='cache/latex-')
15
%% end.rcode
16
17
\begin{document}
18
19
\title{Rtex Knitr in CoCalc}
20
21
\author{Author Name}
22
23
\maketitle
24
25
Boring stuff as usual:
26
27
%% a chunk with default options
28
%% begin.rcode
29
% 1+1
30
%
31
% x=rnorm(5); t(x)
32
%% end.rcode
33
34
For the cached chunk below, you will need to wait for 3 seconds for
35
the first time you compile this document, but it takes no time the
36
next time you run it again.
37
38
%% chunk options: cache this chunk
39
%% begin.rcode my-cache, cache=TRUE
40
% set.seed(123)
41
% x = runif(10)
42
% sd(x) # standard deviation
43
%
44
% Sys.sleep(3) # test cache
45
%% end.rcode
46
47
Now we know the first element of x is \rinline{x[1]}.
48
And we also know the 26 letters are \rinline{LETTERS}.
49
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,
50
i.~e. now the first element of x becomes \rinline{x[1]}.
51
52
How about figures? Let's use the Cairo PDF device (assumes R $\geq$ 2.14.0).
53
54
%% begin.rcode cairo-scatter, dev='cairo_pdf', fig.width=5, fig.height=5, out.width='.8\\textwidth'
55
% plot(cars) # a scatter plot
56
%% end.rcode
57
58
Warnings, messages and errors are preserved by default.
59
60
%% begin.rcode
61
% sqrt(-1) # here is a warning!
62
% message('this is a message you should know')
63
% 1+'a' # impossible
64
%% end.rcode
65
66
\end{document}
67