forked from yihui/knitr-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDR-Yihui-Xie-Chap1-3.lyx
4944 lines (3557 loc) · 81.8 KB
/
DDR-Yihui-Xie-Chap1-3.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.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass krantz
\begin_preamble
\usepackage{tocloft}
\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.75}
% for andre simon's highlight
\let\hlslc\hlcom \let\hlppc\hlcom
\let\hlesc\hlstd \let\hlpps\hlstd \let\hllin\hlstd
\usepackage{emptypage}
\end_preamble
\use_default_options true
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
\language english
\language_package none
\inputencoding utf8
\fontencoding global
\font_roman palatino
\font_sans lmss
\font_typewriter lmtt
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\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 true
\pdf_bookmarksopen true
\pdf_bookmarksopenlevel 3
\pdf_breaklinks true
\pdf_pdfborder false
\pdf_colorlinks false
\pdf_backref false
\pdf_pdfusetitle true
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine natbib_authoryear
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 2
\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 Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<setup, include=FALSE>>=
\end_layout
\begin_layout Plain Layout
library(knitr)
\end_layout
\begin_layout Plain Layout
options(width=55, formatR.arrow=TRUE)
\end_layout
\begin_layout Plain Layout
opts_chunk$set(cache=TRUE, dev='tikz', fig.path='figure-book/', cache.path='cache-
book/', fig.width=5, fig.height=5,
\end_layout
\begin_layout Plain Layout
fig.align='center', error=FALSE, tidy=TRUE)
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
knit_hooks$set(par = function(before, options, envir) {
\end_layout
\begin_layout Plain Layout
if (before && options$fig.show != "none") {
\end_layout
\begin_layout Plain Layout
par(mar = c(4, 4, 0.1, 0.1), cex.lab = 0.95, cex.axis = 0.9, mgp = c(2,
0.7,
\end_layout
\begin_layout Plain Layout
0), tcl = -0.3, las = 1)
\end_layout
\begin_layout Plain Layout
if (is.list(options$par))
\end_layout
\begin_layout Plain Layout
do.call(par, options$par)
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
}, document = function(x) {
\end_layout
\begin_layout Plain Layout
x = gsub('(
\backslash
\backslash
\backslash
\backslash
begin
\backslash
\backslash
{tabular
\backslash
\backslash
})', '
\backslash
\backslash
\backslash
\backslash
medskip{}
\backslash
n
\backslash
\backslash
1', hook_movecode(x))
\end_layout
\begin_layout Plain Layout
gsub('
\backslash
\backslash
\backslash
\backslash
end
\backslash
\backslash
{alltt
\backslash
\backslash
}
\backslash
n*
\backslash
\backslash
\backslash
\backslash
begin
\backslash
\backslash
{alltt
\backslash
\backslash
}
\backslash
n', '', x)
\end_layout
\begin_layout Plain Layout
})
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Title
Dynamic Documents with R and knitr
\end_layout
\begin_layout Author
Yihui Xie
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
pagenumbering{gobble}
\end_layout
\end_inset
\end_layout
\begin_layout Chapter*
Note
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
pagenumbering{roman}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
This PDF document only contains the first three chapters, released with
permission of Chapman & Hall.
The complete book is available on Amazon (
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.amazon.com/gp/product/1482203537
\end_layout
\end_inset
).
\end_layout
\begin_layout Standard
For those who are interested in writing a book with Chapman & Hall, the
LyX and Rnw source files are freely available in the repository
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
https://github.com/yihui/knitr-book
\end_layout
\end_inset
.
Hopefully this template can help beginners eliminate 90% of the possible
LaTeX problems, e.g.,
\end_layout
\begin_layout Itemize
make odd-numbered pages recto, and even-numbered pages verso
\end_layout
\begin_layout Itemize
leave a desired number of blank pages (note that eventually you should remove
this page, since this page is here only for trim marks to be correctly
generated on blank pages)
\end_layout
\begin_layout Itemize
different page numbering styles
\end_layout
\begin_layout Itemize
correct order of the items in the table of contents
\end_layout
\begin_layout Itemize
blank pages after each chapter
\end_layout
\begin_layout Itemize
...
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\begin_inset space ~
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
thispagestyle{empty}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\align center
\size larger
To my parents
\begin_inset VSpace bigskip
\end_inset
\begin_inset Newline newline
\end_inset
Shaobai Xie
\emph on
and
\emph default
Guolan Xie
\end_layout
\begin_layout Standard
\begin_inset Newpage newpage
\end_inset
\end_layout
\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents
\end_inset
\end_layout
\begin_layout Chapter*
Preface
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
addcontentsline{toc}{chapter}{Preface}
\end_layout
\end_inset
We import a dataset into a statistical software package, run a procedure
to get all results, then copy and paste selected pieces into a typesetting
program, add a few descriptions, and finish a report.
This is a common practice of writing statistical reports.
There are obvious dangers and disadvantages in this process:
\end_layout
\begin_layout Enumerate
it is error-prone due to too much manual work;
\end_layout
\begin_layout Enumerate
it requires lots of human effort to do tedious jobs such as copying results
across documents;
\end_layout
\begin_layout Enumerate
the workflow is barely recordable especially when it involves GUI (Graphical
User Interface) operations, therefore it is difficult to reproduce;
\end_layout
\begin_layout Enumerate
a tiny change of the data source in the future will require the author(s)
to go through the same procedure again, which can take nearly the same
amount of time and effort;
\end_layout
\begin_layout Enumerate
the analysis and writing are separate, so close attention has to be paid
to the synchronization of the two parts.
\end_layout
\begin_layout Standard
In fact, a report can be generated dynamically from program code.
Just like a software package has its source code, a dynamic document is
the source code of a report.
It is a combination of computer code and the corresponding narratives.
When we compile the dynamic document, the program code in it is executed
and replaced with the output; we get a final report by mixing the code
output with the narratives.
Because we only manage the source code, we are free of all the possible
problems above.
For example, we can change a single parameter in the source code, and get
a different report on the fly.
\end_layout
\begin_layout Standard
In this book,
\emph on
dynamic documents
\emph default
refer to the kind of source documents containing both program code and
narratives.
Sometimes we may just call them
\emph on
source documents
\emph default
since
\begin_inset Quotes eld
\end_inset
dynamic
\begin_inset Quotes erd
\end_inset
may sound confusing and ambiguous to some people (it does not mean interactivit
y or animations).
We also use the term
\emph on
report
\emph default
frequently throughout the book, which really means the output document
compiled from a dynamic document.
\end_layout
\begin_layout Section*
Who Should Read This Book
\end_layout
\begin_layout Standard
This book is written for both beginners and advanced users.
The main goal is to make writing reports easier: the
\begin_inset Quotes eld
\end_inset
report
\begin_inset Quotes erd
\end_inset
here can range from student homework or project reports, exams, books,
blogs, and web pages to virtually any documents related to statistical
graphics, computing, and data analysis.
\end_layout
\begin_layout Standard
For beginners, Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:intro"
\end_inset
to
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:cache"
\end_inset
should be enough for basic applications (which have already covered many
features); for power users, Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:reference"
\end_inset
to
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:engines"
\end_inset
can be helpful for understanding the extensibility of the
\series bold
knitr
\series default
package.
\end_layout
\begin_layout Standard
Familiarity with LaTeX and HTML can be helpful, but is not required at all;
once we get the basic idea, we can write reports in simple languages such
as Markdown.
Unless otherwise noted, all features apply to all document formats, although
we primarily use LaTeX for examples.
\end_layout
\begin_layout Standard
We recommend the readers to take a look at the Web site RPubs (
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://rpubs.com
\end_layout
\end_inset
), which contains a large number of user-contributed documents.
Hopefully they are convincing enough to show it is quick and easy to write
dynamic documents.
\end_layout
\begin_layout Section*
Software Information and Conventions
\end_layout
\begin_layout Standard
The main tools we introduce in this book are the R language
\begin_inset CommandInset citation
LatexCommand citep
key "R-base"
\end_inset
and the
\series bold
knitr
\series default
package
\begin_inset CommandInset citation
LatexCommand citep
key "R-knitr"
\end_inset
, with which this book was written, but the language in the documents is
not restricted to R; for example, we can also integrate Python, awk, and
shell scripts, etc., into the reports.
For document formats, we mainly use LaTeX, HTML, and Markdown.
\end_layout
\begin_layout Standard
Both R and
\series bold
knitr
\series default
are available on CRAN (Comprehensive R Archive Network) as free and open-source
software:
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://cran.r-project.org
\end_layout
\end_inset
.
Their version information for this book can be found in the session information
:
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<session-info, cache=FALSE>>=
\end_layout
\begin_layout Plain Layout
print(sessionInfo(), FALSE)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The
\series bold
knitr
\series default
package is documented on the Web site
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://yihui.name/knitr/
\end_layout
\end_inset
, and the most important page is perhaps
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://yihui.name/knitr/options
\end_layout
\end_inset
, where we can find the complete reference for chunk options (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:chunk-options"
\end_inset
).
The development version is hosted on Github:
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
https://github.com/yihui/knitr
\end_layout
\end_inset
; you can always check out the latest development version, file issues/feature
requests, or even participate in the development by forking the repository
and making changes by yourself.
There are plenty of examples in the repository
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
https://github.com/yihui/knitr-examples
\end_layout
\end_inset
, including both minimal and advanced examples.
There is also a wiki page maintained by Frank Harrell
\emph on
et al.
\emph default
from the Department of Biostatistics, Vanderbilt University, which introduced
several tricks and useful experience of using
\series bold
knitr
\series default
:
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://biostat.mc.vanderbilt.edu
\end_layout
\end_inset
.
\end_layout
\begin_layout Standard
Unlike many other books on R, we do not add prompts to R source code in
this book, and we comment out the text output by two hashes
\family typewriter
##
\family default
by default, as you can see above.
The reason for this convention is explained in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:text"
\end_inset
.
Package names are in bold text (e.g.,
\series bold
rpart
\series default
), function names in italic (e.g.,
\emph on
paste()
\emph default
), inline code is formatted in a typewriter font (e.g.,
\family typewriter
mean(1:10, trim = 0.1)
\family default
), and filenames are in sans serif fonts (e.g.,
\family sans
figure/foo.pdf
\family default
).
\end_layout
\begin_layout Section*
Structure of the Book
\end_layout
\begin_layout Standard
Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:intro"
\end_inset
is an overview of dynamic documents, introducing the idea of literate programmi
ng; Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:rr"
\end_inset
explains why dynamic documents are important to scientific research from
the viewpoint of reproducible research; Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:first"
\end_inset
gives a first complete example that covers basic concepts and what we can
do with
\series bold
knitr
\series default
; Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:editors"
\end_inset
introduces a few common text editors that support
\series bold
knitr
\series default
, so that it is easier to compile reports from source documents; and Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:formats"
\end_inset
describes the syntax for different document formats such as LaTeX, HTML,
and Markdown.
\end_layout
\begin_layout Standard
Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "chap:text"