| Hosted by CoCalc | Download
1
\documentclass{article}\usepackage[]{graphicx}\usepackage[]{xcolor}
2
% maxwidth is the original width if it is less than linewidth
3
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
4
\makeatletter
5
\def\maxwidth{ %
6
\ifdim\Gin@nat@width>\linewidth
7
\linewidth
8
\else
9
\Gin@nat@width
10
\fi
11
}
12
\makeatother
13
14
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
15
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
16
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
17
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
18
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
19
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
20
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
21
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
22
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
23
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
24
\let\hlipl\hlkwb
25
26
\usepackage{framed}
27
\makeatletter
28
\newenvironment{kframe}{%
29
\def\at@end@of@kframe{}%
30
\ifinner\ifhmode%
31
\def\at@end@of@kframe{\end{minipage}}%
32
\begin{minipage}{\columnwidth}%
33
\fi\fi%
34
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
35
\colorbox{shadecolor}{##1}\hskip-\fboxsep
36
% There is no \\@totalrightmargin, so:
37
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
38
\MakeFramed {\advance\hsize-\width
39
\@totalleftmargin\z@ \linewidth\hsize
40
\@setminipage}}%
41
{\par\unskip\endMakeFramed%
42
\at@end@of@kframe}
43
\makeatother
44
45
\definecolor{shadecolor}{rgb}{.97, .97, .97}
46
\definecolor{messagecolor}{rgb}{0, 0, 0}
47
\definecolor{warningcolor}{rgb}{1, 0, 1}
48
\definecolor{errorcolor}{rgb}{1, 0, 0}
49
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
50
51
\usepackage{alltt}
52
\usepackage[utf8]{inputenc}
53
\usepackage[T1]{fontenc}
54
\usepackage{url}
55
56
57
\title{Knitr in CoCalc}
58
59
\author{Harald Schilly}
60
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
61
\begin{document}
62
63
\maketitle
64
65
You can type R commands in your \LaTeX{} document and they will be properly run and the output printed in the document.
66
67
\begin{knitrout}
68
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
69
\begin{alltt}
70
\hlkwd{library}\hlstd{(knitr)}
71
\hlstd{opts_chunk}\hlopt{$}\hlkwd{set}\hlstd{(}\hlkwc{fig.path}\hlstd{=}\hlstr{'figure/latex-'}\hlstd{,} \hlkwc{cache.path}\hlstd{=}\hlstr{'cache/latex-'}\hlstd{)}
72
\end{alltt}
73
\end{kframe}
74
\end{knitrout}
75
76
\begin{knitrout}
77
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
78
\begin{alltt}
79
\hlstd{R.version}
80
\end{alltt}
81
\begin{verbatim}
82
## _
83
## platform x86_64-pc-linux-gnu
84
## arch x86_64
85
## os linux-gnu
86
## system x86_64, linux-gnu
87
## status
88
## major 4
89
## minor 3.0
90
## year 2023
91
## month 04
92
## day 21
93
## svn rev 84292
94
## language R
95
## version.string R version 4.3.0 (2023-04-21)
96
## nickname Already Tomorrow
97
\end{verbatim}
98
\end{kframe}
99
\end{knitrout}
100
101
\begin{knitrout}
102
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
103
\begin{alltt}
104
\hlstd{ip} \hlkwb{<-} \hlkwd{installed.packages}\hlstd{()}
105
\hlkwd{dim}\hlstd{(ip)}
106
\end{alltt}
107
\begin{verbatim}
108
## [1] 5470 16
109
\end{verbatim}
110
\end{kframe}
111
\end{knitrout}
112
113
\begin{knitrout}
114
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
115
\begin{alltt}
116
\hlcom{# Create a sequence of numbers}
117
\hlstd{X} \hlkwb{=} \hlnum{2}\hlopt{:}\hlnum{10}
118
119
\hlcom{# Display basic statistical measures}
120
\hlkwd{summary}\hlstd{(X)}
121
\end{alltt}
122
\begin{verbatim}
123
## Min. 1st Qu. Median Mean 3rd Qu. Max.
124
## 2 4 6 6 8 10
125
\end{verbatim}
126
\end{kframe}
127
\end{knitrout}
128
129
\begin{knitrout}
130
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
131
\begin{alltt}
132
\hlstd{myF} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{) \{}
133
\hlkwd{print}\hlstd{(}\hlnum{2}\hlopt{*}\hlstd{x} \hlopt{+} \hlnum{1}\hlstd{)}
134
\hlstd{\}}
135
\hlkwd{myF}\hlstd{(}\hlnum{22}\hlstd{)}
136
\end{alltt}
137
\begin{verbatim}
138
## [1] 45
139
\end{verbatim}
140
\end{kframe}
141
\end{knitrout}
142
143
$$x^y$$
144
145
\begin{knitrout}
146
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
147
\begin{alltt}
148
\hlstd{a} \hlkwb{<-} \hlnum{111}
149
\end{alltt}
150
\end{kframe}
151
\end{knitrout}
152
153
\begin{knitrout}
154
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
155
\begin{alltt}
156
\hlstd{x} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlnum{2}\hlstd{,}\hlnum{3}\hlstd{,}\hlnum{4}\hlstd{,}\hlnum{5}\hlstd{,}\hlnum{1}\hlstd{,}\hlnum{2}\hlstd{,}\hlnum{3}\hlstd{,}\hlnum{2}\hlstd{)}
157
\hlkwd{summary}\hlstd{(x)}
158
\end{alltt}
159
\begin{verbatim}
160
## Min. 1st Qu. Median Mean 3rd Qu. Max.
161
## 1.00 2.00 2.50 2.75 3.25 5.00
162
\end{verbatim}
163
\end{kframe}
164
\end{knitrout}
165
166
\begin{knitrout}
167
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
168
\begin{alltt}
169
\hlkwd{hist}\hlstd{(x)}
170
\end{alltt}
171
\end{kframe}
172
\includegraphics[width=.75\linewidth]{figure/latex-histogram-plot-1}
173
\end{knitrout}
174
175
Sum of 2+3+4+5+1+2+3+2 is 22.
176
177
test
178
179
test 123
180
181
test
182
183
test
184
185
test
186
187
test
188
189
test
190
191
test
192
193
test
194
195
test
196
197
test
198
199
test
200
201
test
202
203
test
204
205
test
206
207
208
209
where am I? here.
210
211
t
212
213
t
214
215
t
216
217
asdf
218
219
t
220
221
t
222
223
t
224
225
t
226
227
t
228
229
t
230
231
t
232
233
t
234
235
t
236
237
t
238
123
239
240
t
241
242
243
this line
244
245
t
246
247
t
248
249
t
250
251
\begin{knitrout}
252
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
253
\begin{alltt}
254
\hlkwd{hist}\hlstd{(x)}
255
\end{alltt}
256
\end{kframe}
257
\includegraphics[width=.4\linewidth]{figure/latex-histogram-plot2-1}
258
\end{knitrout}
259
260
t
261
262
t
263
264
t
265
266
ok
267
268
t
269
270
t
271
272
t
273
274
t
275
276
t
277
278
t
279
280
\begin{knitrout}
281
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
282
\begin{alltt}
283
\hlkwd{hist}\hlstd{(x)}
284
\end{alltt}
285
\end{kframe}
286
\includegraphics[width=.4\linewidth]{figure/latex-histogram-plot3-1}
287
\end{knitrout}
288
t
289
290
t
291
292
t
293
294
t
295
296
t
297
298
t
299
300
t
301
302
t
303
304
t
305
306
\pagebreak
307
308
\begin{knitrout}
309
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
310
\begin{alltt}
311
\hlstd{data} \hlkwb{<-} \hlkwd{rnorm}\hlstd{(}\hlnum{1000}\hlstd{)}
312
\hlkwd{hist}\hlstd{(data)}
313
\end{alltt}
314
\end{kframe}
315
\includegraphics[width=.9\linewidth]{figure/latex-histogram-plot4-1}
316
\end{knitrout}
317
318
0.0588235 and \ensuremath{1.23\times 10^{-6}}.
319
320
EOF
321
322
\end{document}
323
324
325
326