-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo.lyx
1568 lines (1065 loc) · 24.4 KB
/
demo.lyx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass article
\begin_preamble
\end_preamble
\use_default_options false
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
\language english
\language_package none
\inputencoding auto
\fontencoding default
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref true
\pdf_bookmarks true
\pdf_bookmarksnumbered false
\pdf_bookmarksopen false
\pdf_bookmarksopenlevel 1
\pdf_breaklinks false
\pdf_pdfborder false
\pdf_colorlinks false
\pdf_backref section
\pdf_pdfusetitle true
\papersize default
\use_geometry true
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 0
\use_package mathtools 1
\use_package mhchem 0
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine natbib
\cite_engine_type authoryear
\biblio_style plainnat
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 0
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 1in
\topmargin 1in
\rightmargin 1in
\bottommargin 1in
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Title
An example \SpecialChar LyX
file
\begin_inset Newline newline
\end_inset
Illustrating use of R, bash, and Python code chunks
\end_layout
\begin_layout Author
Christopher Paciorek
\end_layout
\begin_layout Date
February 2022
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<setup, include=FALSE>>=
\end_layout
\begin_layout Plain Layout
# include any code here you don't want to show up in the document,
\end_layout
\begin_layout Plain Layout
# e.g., package and dataset loading
\end_layout
\begin_layout Plain Layout
require(ggplot2)
\end_layout
\begin_layout Plain Layout
set.seed(1)
\end_layout
\begin_layout Plain Layout
# also a good place to set global chunk options
\end_layout
\begin_layout Plain Layout
library(knitr) # need this for opts_chunk command
\end_layout
\begin_layout Plain Layout
opts_chunk$set(fig.width = 5, fig.height = 5)
\end_layout
\begin_layout Plain Layout
# if we wanted chunks by default not to be evaluated
\end_layout
\begin_layout Plain Layout
# opts_chunk$set(eval = FALSE)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Section
How to generate a document from this file
\end_layout
\begin_layout Standard
If you open this file in \SpecialChar LyX
, you can run this document through the
\emph on
knitr
\emph default
package for R to generate a PDF using
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
begin{verbatim}
\end_layout
\begin_layout Plain Layout
File -> Export -> PDF (pdflatex)
\end_layout
\begin_layout Plain Layout
\backslash
end{verbatim}
\end_layout
\end_inset
There are other options using that same menu to export in a variety of formats,
including HTML.
\end_layout
\begin_layout Standard
Note that to use code chunks in \SpecialChar LyX
, you need to enable the
\emph on
knitr
\emph default
module in the file.
After opening a new or existing \SpecialChar LyX
document, you'll need to go to
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
begin{verbatim}
\end_layout
\begin_layout Plain Layout
Document -> Settings -> Modules
\end_layout
\begin_layout Plain Layout
\backslash
end{verbatim}
\end_layout
\end_inset
and select
\family typewriter
Rnw (knitr)
\family default
from the
\family typewriter
Available
\family default
list and click on
\family typewriter
Add
\family default
to add it to the
\family typewriter
Selected
\family default
list.
Once you do this, you'll see that
\family typewriter
Chun
\family default
k will show up in the pull-down menu listing the available formats.
You can then tags lines of code with the
\family typewriter
Chunk
\family default
tag, so it gets evaluated as code.
\end_layout
\begin_layout Section
\SpecialChar LaTeX
\end_layout
\begin_layout Standard
This document will focus on embedding code and not on standard \SpecialChar LaTeX
or \SpecialChar LyX
.
For a quick introduction to \SpecialChar LaTeX
, including some details on \SpecialChar LyX
, see
\begin_inset CommandInset href
LatexCommand href
name "our tutorial"
target "https://github.com/berkeley-scf/tutorial-latex-intro"
literal "false"
\end_inset
.
\end_layout
\begin_layout Standard
The process for producing an output document described in the previous section
processes the \SpecialChar LyX
+ Sweave (Rnw) format, evaluating the R code chunks, and
creating a standard \SpecialChar LaTeX
file with the code snippets and output created by
the code properly formatted as standard \SpecialChar LaTeX
, before compiling the \SpecialChar LaTeX
file to
PDF.
\end_layout
\begin_layout Standard
Note that if you wish to embed pure \SpecialChar LaTeX
into a \SpecialChar LyX
document, that's easy to do
with the \SpecialChar TeX
button on the \SpecialChar LyX
toolbar.
So you can always fall back on using what you know about \SpecialChar LaTeX
even if you're
not sure how to do something in \SpecialChar LyX
.
\end_layout
\begin_layout Section
Embedding equations using \SpecialChar LaTeX
\end_layout
\begin_layout Standard
\SpecialChar LyX
is a front end for \SpecialChar LaTeX
, so you can just use \SpecialChar LaTeX
math syntax after entering math
mode.
\end_layout
\begin_layout Standard
Here is an inline equation
\begin_inset Formula $f(x)=\int f(y,x)dy$
\end_inset
, where I access math mode by entering
\family typewriter
Ctrl-m
\family default
(note the lowercase
\begin_inset Quotes eld
\end_inset
m
\begin_inset Quotes erd
\end_inset
).
\end_layout
\begin_layout Standard
Here's a displayed equation, where I enter math mode by entering
\family typewriter
Ctrl-M
\family default
(note the upper-case
\begin_inset Quotes eld
\end_inset
M
\begin_inset Quotes erd
\end_inset
):
\begin_inset Formula
\[
f_{\theta}(x)=\int f_{\theta}(y,x)dy.
\]
\end_inset
\end_layout
\begin_layout Section
Embedding R code
\end_layout
\begin_layout Standard
Here's an R code chunk using standard Sweave formatting.
To create a chunk, select
\family typewriter
Insert -> \SpecialChar TeX
code
\family default
(or use the \SpecialChar TeX
button in the \SpecialChar LyX
toolbar) and then type in your code as seen
here.
\begin_inset Note Note
status open
\begin_layout Plain Layout
Alternatively, rather than inserting as \SpecialChar TeX
code, you can tag your code using
the Chunk tag from the pulldown menu at the left of the toolbar near the
top of the \SpecialChar LyX
window – in that case, see
\emph on
demo_alt.lyx
\emph default
).
NOTE: as of August 2017 I'm noticing that the chunk tag doesn't seem to
be available in more recent versions of \SpecialChar LyX
.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<r-chunk1>>=
\end_layout
\begin_layout Plain Layout
a <- c(7, 3)
\end_layout
\begin_layout Plain Layout
mean(a)
\end_layout
\begin_layout Plain Layout
b <- a + 3
\end_layout
\begin_layout Plain Layout
mean(b)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
noindent
\end_layout
\end_inset
Unfortunately, we need to use the
\emph on
noindent
\emph default
command (in \SpecialChar LaTeX
mode) to prevent the text after a chunk from being considered
as a new paragraph.
Here's another chunk:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<r-chunk2>>=
\end_layout
\begin_layout Plain Layout
mean(b)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
When running R code, output is printed interspersed with the code, as one
would generally want.
Also, later chunks have access to result from earlier chunks (i.e., state
is preserved between chunks).
\end_layout
\begin_layout Standard
Let's make a plot
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<r-plot, fig.height=3>>=
\end_layout
\begin_layout Plain Layout
hist(rnorm(20))
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
And here's some inline R code: What is 3 plus 5?
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
Sexpr{3+5}
\end_layout
\end_inset
.
Note that we need to use the
\family typewriter
\backslash
Sexpr
\family default
command within a \SpecialChar LaTeX
environment, so we need to select the command and use
the \SpecialChar TeX
button in the toolbar.
\end_layout
\begin_layout Standard
You have control over whether code in chunks is echoed into the document
and evaluated using the
\emph on
include
\emph default
,
\emph on
echo
\emph default
, and
\emph on
eval
\emph default
flags.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<include, include=FALSE>>=
\end_layout
\begin_layout Plain Layout
cat("This code is evaluated, but nothing is printed in the document.")
\end_layout
\begin_layout Plain Layout
library(fields)
\end_layout
\begin_layout Plain Layout
# fields package should now be loaded for use by later chunks
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<echo, echo=FALSE>>=
\end_layout
\begin_layout Plain Layout
cat("This code is not printed in the document, but results of evaluating
the code are printed.")
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<eval, eval=FALSE>>=
\end_layout
\begin_layout Plain Layout
cat("This code is not evaluated, but the code itself is printed in the document."
)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Intensive calculations can be saved using the
\family typewriter
cache=TRUE
\family default
tag so they don't need to be rerun every time you compile the document.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<slow-step, cache=TRUE>>=
\end_layout
\begin_layout Plain Layout
mean(rnorm(5e7))
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Section
Embedding bash and Python code
\end_layout
\begin_layout Subsection
bash
\end_layout
\begin_layout Standard
A bash chunk:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<bash-chunk1, engine='bash'>>=
\end_layout
\begin_layout Plain Layout
ls -l
\end_layout
\begin_layout Plain Layout
df -h
\end_layout
\begin_layout Plain Layout
cd /tmp
\end_layout
\begin_layout Plain Layout
pwd
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Unfortunately, output from bash chunks occurs after all the code is printed.
Also, state is not preserved between chunks.
\end_layout
\begin_layout Standard
We can see that state is not preserved here, where the current working directory
is NOT the directory that we changed to in the chunk above.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<bash-chunk2, engine='bash'>>=
\end_layout
\begin_layout Plain Layout
pwd
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Inline bash code won't work.
\end_layout
\begin_layout Subsection
Embedding Python code
\end_layout
\begin_layout Standard
You can embed Python code.
As with R, state is preserved so later chunks can use objects from earlier
chunks.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<py-chunk1, engine='python'>>=
\end_layout
\begin_layout Plain Layout
import numpy as np
\end_layout
\begin_layout Plain Layout
x = np.array((3, 5, 7))
\end_layout
\begin_layout Plain Layout
x.sum()
\end_layout
\begin_layout Plain Layout
x.min()
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<py-chunk2, engine='python'>>=
\end_layout
\begin_layout Plain Layout
try:
\end_layout
\begin_layout Plain Layout
x[0]
\end_layout
\begin_layout Plain Layout
except NameError:
\end_layout
\begin_layout Plain Layout
print('x does not exist')
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
There is no facility for inline Python code evaluation.
\end_layout
\begin_layout Section
Reading code from an external file
\end_layout
\begin_layout Standard
It's sometimes nice to draw code in from a separate file.
Note that a good place for reading the source file via
\family typewriter
read
\backslash
_chunk()
\family default
is in an initial setup chunk at the beginning of the document.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<read-chunk, echo=FALSE>>=
\end_layout
\begin_layout Plain Layout
library(knitr)
\end_layout
\begin_layout Plain Layout
read_chunk('demo.R')
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<external_chunk_1>>=
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<external_chunk_2>>=
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Section
Formatting of long lines of code and of output
\end_layout
\begin_layout Subsection
R code
\end_layout
\begin_layout Standard
Having long lines be nicely formatted and other aspects of formatting can
be a challenge.
Also, results can differ depending on your output format (e.g., PDF vs.
HTML).
In general the code in this section will often overflow the page width
in PDF but not in HTML, but even in the HTML the line breaks may be awkwardly
positioned.
\end_layout
\begin_layout Standard
Here are some examples that overflow in PDF output.
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout