]> git.lyx.org Git - lyx.git/blob - lib/examples/Modules/Sweave.lyx
Support MLA's \autocite variants
[lyx.git] / lib / examples / Modules / Sweave.lyx
1 #LyX 2.4 created this file. For more info see https://www.lyx.org/
2 \lyxformat 620
3 \begin_document
4 \begin_header
5 \save_transient_properties true
6 \origin /systemlyxdir/examples/Modules/
7 \textclass article
8 \begin_preamble
9 %% maxwidth is the original width if it's less than linewidth
10 %% otherwise use linewidth (to make sure the graphics do not exceed the margin)
11 \def\maxwidth{%
12 \ifdim\Gin@nat@width>\linewidth
13 \linewidth
14 \else
15 \Gin@nat@width
16 \fi
17 }
18 \end_preamble
19 \options nogin
20 \use_default_options true
21 \begin_modules
22 sweave
23 \end_modules
24 \maintain_unincluded_children no
25 \language english
26 \language_package auto
27 \inputencoding utf8
28 \fontencoding auto
29 \font_roman "palatino" "default"
30 \font_sans "lmss" "default"
31 \font_typewriter "lmtt" "default"
32 \font_math "auto" "auto"
33 \font_default_family default
34 \use_non_tex_fonts false
35 \font_sc false
36 \font_roman_osf false
37 \font_sans_osf false
38 \font_typewriter_osf false
39 \font_sf_scale 100 100
40 \font_tt_scale 100 100
41 \use_microtype false
42 \use_dash_ligatures false
43 \graphics default
44 \default_output_format default
45 \output_sync 0
46 \bibtex_command default
47 \index_command default
48 \paperfontsize default
49 \spacing single
50 \use_hyperref false
51 \papersize default
52 \use_geometry true
53 \use_package amsmath 1
54 \use_package amssymb 1
55 \use_package cancel 1
56 \use_package esint 1
57 \use_package mathdots 1
58 \use_package mathtools 1
59 \use_package mhchem 1
60 \use_package stackrel 1
61 \use_package stmaryrd 1
62 \use_package undertilde 1
63 \cite_engine basic
64 \cite_engine_type default
65 \biblio_style plain
66 \use_bibtopic false
67 \use_indices false
68 \paperorientation portrait
69 \suppress_date true
70 \justification true
71 \use_refstyle 0
72 \use_minted 0
73 \use_lineno 0
74 \index Index
75 \shortcut idx
76 \color #008000
77 \end_index
78 \leftmargin 3cm
79 \topmargin 3cm
80 \rightmargin 3cm
81 \bottommargin 3cm
82 \secnumdepth 3
83 \tocdepth 3
84 \paragraph_separation indent
85 \paragraph_indentation default
86 \is_math_indent 0
87 \math_numbering_side default
88 \quotes_style english
89 \dynamic_quotes 0
90 \papercolumns 1
91 \papersides 1
92 \paperpagestyle default
93 \tablestyle default
94 \tracking_changes false
95 \output_changes false
96 \change_bars false
97 \postpone_fragile_content false
98 \html_math_output 0
99 \html_css_as_file 0
100 \html_be_strict false
101 \docbook_table_output 0
102 \docbook_mathml_prefix 1
103 \end_header
104
105 \begin_body
106
107 \begin_layout Standard
108 \begin_inset Note Note
109 status open
110
111 \begin_layout Plain Layout
112 \begin_inset Flex Sweave Options
113 status open
114
115 \begin_layout Plain Layout
116
117 prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE
118 \end_layout
119
120 \end_inset
121
122
123 \end_layout
124
125 \end_inset
126
127
128 \end_layout
129
130 \begin_layout Standard
131 \begin_inset ERT
132 status open
133
134 \begin_layout Plain Layout
135
136 %% 
137 \backslash
138 maxwidth was defined in the preamble;
139  see document settings
140 \end_layout
141
142 \begin_layout Plain Layout
143
144
145 \backslash
146 setkeys{Gin}{width=
147 \backslash
148 maxwidth}
149 \end_layout
150
151 \end_inset
152
153
154 \end_layout
155
156 \begin_layout Title
157 Using Sweave with \SpecialChar LyX
158
159 \end_layout
160
161 \begin_layout Author
162 Yihui Xie
163 \begin_inset Foot
164 status collapsed
165
166 \begin_layout Plain Layout
167 Department of Statistics,
168  Iowa State University.
169  Email:
170  
171 \begin_inset CommandInset href
172 LatexCommand href
173 name "xie@yihui.name"
174 target "xie@yihui.name"
175 type "mailto:"
176 literal "false"
177
178 \end_inset
179
180
181 \end_layout
182
183 \end_inset
184
185  
186 \begin_inset ERT
187 status collapsed
188
189 \begin_layout Plain Layout
190
191
192 \backslash
193 and
194 \end_layout
195
196 \end_inset
197
198  Gregor Gorjanc 
199 \begin_inset ERT
200 status collapsed
201
202 \begin_layout Plain Layout
203
204
205 \backslash
206 and
207 \end_layout
208
209 \end_inset
210
211  Jean-Marc Lasgouttes
212 \end_layout
213
214 \begin_layout Standard
215 Sweave is a tool in the R language (
216 \begin_inset Flex URL
217 status collapsed
218
219 \begin_layout Plain Layout
220
221 http://www.r-project.org
222 \end_layout
223
224 \end_inset
225
226 ) for dynamic report generation using the ideas of literate programming.
227  R code can be mixed into a \SpecialChar LaTeX
228  document,
229  and will be executed in R when the document is compiled;
230  the results are written into the \SpecialChar TeX
231  output.
232 \end_layout
233
234 \begin_layout Standard
235 Since \SpecialChar LyX
236  2.0,
237  Sweave can be used out of the box with any document class using the 
238 \family sans
239 Sweave
240 \family default
241  module.
242  We can add the 
243 \family sans
244 Sweave
245 \family default
246  module to a \SpecialChar LyX
247  document from 
248 \family sans
249 Document\SpecialChar menuseparator
250 Settings\SpecialChar menuseparator
251 Modules
252 \family default
253 .
254  In this manual,
255  we demonstrate some common features of Sweave and explain how the module works.
256  A PDF version of this manual can be found at 
257 \begin_inset Flex URL
258 status collapsed
259
260 \begin_layout Plain Layout
261
262 https://github.com/downloads/yihui/lyx/sweave.pdf
263 \end_layout
264
265 \end_inset
266
267 .
268 \end_layout
269
270 \begin_layout Standard
271 Since \SpecialChar LyX
272  2.3,
273  it is necessary to enable the use of 
274 \emph on
275 needauth
276 \emph default
277  converters in
278 \family sans
279  Preferences\SpecialChar menuseparator
280 File Handling\SpecialChar menuseparator
281 Converters
282 \family default
283  in order to compile with Sweave.
284 \end_layout
285
286 \begin_layout Section
287 System Requirements
288 \end_layout
289
290 \begin_layout Standard
291 The 
292 \family sans
293 Sweave
294 \family default
295  module depends on the executable 
296 \family typewriter
297 Rscript
298 \family default
299 ,
300  so it has to be in the 
301 \family sans
302 PATH
303 \family default
304  variable of the system.
305  It is usually not a problem for Linux and Mac OS users,
306  but may be confusing to Windows users;
307  if you do not understand what is 
308 \family sans
309 PATH
310 \family default
311  under Windows,
312  you may either use the R script 
313 \family sans
314 add-R-path-win.R
315 \family default
316  in the repository 
317 \begin_inset Flex URL
318 status collapsed
319
320 \begin_layout Plain Layout
321
322 https://github.com/yihui/lyx
323 \end_layout
324
325 \end_inset
326
327  to change it automatically (not recommended;
328  you should learn 
329 \family sans
330 PATH
331 \family default
332  anyway),
333  or go to 
334 \family sans
335 Tools\SpecialChar menuseparator
336 Preferences\SpecialChar menuseparator
337 Paths\SpecialChar menuseparator
338 PATH prefix
339 \family default
340  to add the bin path of R (recommended),
341  which is often like 
342 \family sans
343 C:
344 \backslash
345 Program Files
346 \backslash
347 R
348 \backslash
349 R-x.x.x
350 \backslash
351 bin
352 \family default
353  and you can find it in R:
354 \end_layout
355
356 \begin_layout Standard
357 \begin_inset Flex Chunk
358 status open
359
360 \begin_layout Plain Layout
361
362 \begin_inset Argument 1
363 status open
364
365 \begin_layout Plain Layout
366
367 where-is-R-bin,eval=FALSE
368 \end_layout
369
370 \end_inset
371
372
373 \end_layout
374
375 \begin_layout Plain Layout
376
377 R.home('bin')
378 \end_layout
379
380 \end_inset
381
382
383 \end_layout
384
385 \begin_layout Standard
386 After you have done these changes,
387  you need to reconfigure \SpecialChar LyX
388  by 
389 \family sans
390 Tools\SpecialChar menuseparator
391 Reconfigure
392 \family default
393 .
394  If 
395 \family typewriter
396 Rscript
397 \family default
398  is not present in 
399 \family sans
400 PATH
401 \family default
402 ,
403  the 
404 \family sans
405 Sweave
406 \family default
407  module will be unavailable.
408  It is recommended to use the latest version of R,
409  since Sweave is still being updated.
410  Another dependency is introduced by the \SpecialChar LaTeX
411  style file 
412 \family sans
413 Sweave.sty
414 \family default
415  which is shipped with R under this path (it is not on CTAN):
416 \end_layout
417
418 \begin_layout Standard
419 \begin_inset Flex Chunk
420 status open
421
422 \begin_layout Plain Layout
423
424 \begin_inset Argument 1
425 status open
426
427 \begin_layout Plain Layout
428
429 sweave-sty,eval=FALSE
430 \end_layout
431
432 \end_inset
433
434
435 \end_layout
436
437 \begin_layout Plain Layout
438
439 file.path(R.home('share'),
440  'texmf',
441  'tex',
442  'latex')
443 \end_layout
444
445 \end_inset
446
447
448 \end_layout
449
450 \begin_layout Standard
451 Mik\SpecialChar TeX
452  users may add the texmf directory to the list of root directories in Mik\SpecialChar TeX
453  through Mik\SpecialChar TeX
454  settings.
455  Further note that 
456 \family sans
457 Sweave.sty
458 \family default
459  depends on the 
460 \series bold
461 fancyvrb
462 \series default
463  package and 
464 \emph on
465 may
466 \emph default
467  depend on 
468 \series bold
469 ae
470 \series default
471 .
472  If \SpecialChar LyX
473  cannot find 
474 \family sans
475 Sweave.sty
476 \family default
477 ,
478  it will directly copy from R,
479  so the Sweave module works even if you do not know where is this style file.
480 \end_layout
481
482 \begin_layout Section
483 Sweave Examples
484 \end_layout
485
486 \begin_layout Standard
487 Before we write an Sweave document,
488  we can set some Sweave options;
489  the command is 
490 \family typewriter
491
492 \backslash
493 SweaveOpts{}
494 \family default
495  in Sweave and it can be inserted in a \SpecialChar LyX
496  document by 
497 \family sans
498 Insert\SpecialChar menuseparator
499 Custom Insets\SpecialChar menuseparator
500 Sweave Options
501 \family default
502 ,
503  or just use \SpecialChar TeX
504  code to write it (
505 \family sans
506 Insert\SpecialChar menuseparator
507 \SpecialChar TeX
508  Code
509 \family default
510 ).
511  For example,
512  we can set a global option 
513 \family typewriter
514 echo=FALSE
515 \family default
516  in the beginning of a document to suppress all R code (by default,
517  both R code and its output will show up in the \SpecialChar LaTeX
518  document).
519  
520 \end_layout
521
522 \begin_layout Standard
523 To write R code in \SpecialChar LyX
524 ,
525  you can first insert 
526 \family typewriter
527 Chunk
528 \family default
529  insets from 
530 \family sans
531 Insert\SpecialChar menuseparator
532 Custom Insets\SpecialChar menuseparator
533 Chunk
534 \family default
535 ,
536  or you can simply open a \SpecialChar TeX
537  code environment and write Sweave code chunks in it.
538  However,
539  \SpecialChar LyX
540  provides a summary of all the 
541 \family typewriter
542 Chunk
543 \family default
544  insets in the document in the 
545 \family sans
546 Navigate\SpecialChar menuseparator
547 Literate Programming
548 \family default
549  and 
550 \family sans
551 View\SpecialChar menuseparator
552 Outline Pane
553 \family default
554  menus.
555 \end_layout
556
557 \begin_layout Standard
558 Some R options can be useful to the Sweave output as well,
559  e.g.
560  
561 \family typewriter
562 width
563 \family default
564  and 
565 \family typewriter
566 useFancyQuotes
567 \family default
568 .
569  The former option can adjust the width of the output,
570  and the latter can avoid a common problem with quotes.
571  Users are encouraged to read the Sweave manual in R for more details.
572 \end_layout
573
574 \begin_layout Standard
575 \begin_inset Flex Chunk
576 status open
577
578 \begin_layout Plain Layout
579
580 \begin_inset Argument 1
581 status open
582
583 \begin_layout Plain Layout
584
585 setup,
586  keep.source=TRUE
587 \end_layout
588
589 \end_inset
590
591
592 \end_layout
593
594 \begin_layout Plain Layout
595
596 ## the option 'digits' can set the (approximate) number of digits
597 \end_layout
598
599 \begin_layout Plain Layout
600
601 options(width = 70,
602  useFancyQuotes = FALSE,
603  digits = 4)
604 \end_layout
605
606 \end_inset
607
608
609 \end_layout
610
611 \begin_layout Standard
612 \begin_inset Flex Chunk
613 status open
614
615 \begin_layout Plain Layout
616
617 \begin_inset Argument 1
618 status open
619
620 \begin_layout Plain Layout
621
622 sweave-manual,
623  eval=FALSE,
624  keep.source=TRUE
625 \end_layout
626
627 \end_inset
628
629
630 \end_layout
631
632 \begin_layout Plain Layout
633
634 ## view the Sweave vignette (requires R >= 2.13.0)
635 \end_layout
636
637 \begin_layout Plain Layout
638
639 vignette("Sweave",
640  package = "utils")
641 \end_layout
642
643 \end_inset
644
645
646 \end_layout
647
648 \begin_layout Standard
649 A simple example that will run in any S engine:
650  
651 \end_layout
652
653 \begin_layout Standard
654 \begin_inset Flex Chunk
655 status open
656
657 \begin_layout Plain Layout
658
659 \begin_inset Argument 1
660 status open
661
662 \begin_layout Plain Layout
663
664 print-integers,
665  keep.source=TRUE
666 \end_layout
667
668 \end_inset
669
670
671 \end_layout
672
673 \begin_layout Plain Layout
674
675 ## the integers from 1 to 10 are
676 \end_layout
677
678 \begin_layout Plain Layout
679
680 1:10
681 \end_layout
682
683 \end_inset
684
685
686 \end_layout
687
688 \begin_layout Standard
689 \begin_inset Flex Chunk
690 status open
691
692 \begin_layout Plain Layout
693
694 \begin_inset Argument 1
695 status open
696
697 \begin_layout Plain Layout
698
699 hide-results,
700  results=hide
701 \end_layout
702
703 \end_inset
704
705
706 \end_layout
707
708 \begin_layout Plain Layout
709
710 ## intentionally hide the output by setting results=hide
711 \end_layout
712
713 \begin_layout Plain Layout
714
715 print(1:20)
716 \end_layout
717
718 \end_inset
719
720
721 \end_layout
722
723 \begin_layout Standard
724 We can also emulate a simple calculator:
725  
726 \end_layout
727
728 \begin_layout Standard
729 \begin_inset Flex Chunk
730 status open
731
732 \begin_layout Plain Layout
733
734 \begin_inset Argument 1
735 status open
736
737 \begin_layout Plain Layout
738
739 calculator
740 \end_layout
741
742 \end_inset
743
744
745 \end_layout
746
747 \begin_layout Plain Layout
748
749 1 + pi
750 \end_layout
751
752 \begin_layout Plain Layout
753
754 sin(pi/6)
755 \end_layout
756
757 \end_inset
758
759
760 \end_layout
761
762 \begin_layout Standard
763 Now we look at some Gaussian data:
764 \end_layout
765
766 \begin_layout Standard
767 \begin_inset Flex Chunk
768 status open
769
770 \begin_layout Plain Layout
771
772 \begin_inset Argument 1
773 status open
774
775 \begin_layout Plain Layout
776
777 rnorm
778 \end_layout
779
780 \end_inset
781
782
783 \end_layout
784
785 \begin_layout Plain Layout
786
787 set.seed(123)  # for reproducibility
788 \end_layout
789
790 \begin_layout Plain Layout
791
792 x <- rnorm(20)
793 \end_layout
794
795 \begin_layout Plain Layout
796
797 print(x)
798 \end_layout
799
800 \begin_layout Plain Layout
801
802 t1 <- t.test(x)
803 \end_layout
804
805 \end_inset
806
807
808 \end_layout
809
810 \begin_layout Standard
811 Note that we can easily integrate some numbers into standard text;
812  this is done with the command 
813 \family typewriter
814
815 \backslash
816 Sexpr{}
817 \family default
818 ,
819  and the corresponding \SpecialChar LyX
820  menu is 
821 \family sans
822 Insert\SpecialChar menuseparator
823 Custom Insets\SpecialChar menuseparator
824 S/R expression
825 \family default
826 ,
827  or alternatively just use \SpecialChar TeX
828  code.
829  For example,
830  the third element of the vector 
831 \family typewriter
832 x
833 \family default
834  is 
835 \begin_inset Flex S/R expression
836 status collapsed
837
838 \begin_layout Plain Layout
839
840 x[3]
841 \end_layout
842
843 \end_inset
844
845  (i.e.
846  
847 \family typewriter
848 x[3]
849 \family default
850 ) and the 
851 \begin_inset Formula $p$
852 \end_inset
853
854 -value of the test is 
855 \begin_inset Flex S/R expression
856 status collapsed
857
858 \begin_layout Plain Layout
859
860 format.pval(t1$p.value)
861 \end_layout
862
863 \end_inset
864
865 .
866  You can round these numbers using functions like 
867 \emph on
868 round()
869 \emph default
870  in R.
871  
872 \end_layout
873
874 \begin_layout Standard
875
876 \family roman
877 \series medium
878 \shape up
879 \size normal
880 \emph off
881 \bar no
882 \noun off
883 \color none
884 Now we look at a summary of the 
885 \family typewriter
886 \series default
887 \shape default
888 \size default
889 \emph default
890 \bar default
891 \noun default
892 \color inherit
893 iris
894 \family roman
895 \series medium
896 \shape up
897 \size normal
898 \emph off
899 \bar no
900 \noun off
901 \color none
902  dataset:
903 \end_layout
904
905 \begin_layout Standard
906 \begin_inset Flex Chunk
907 status open
908
909 \begin_layout Plain Layout
910
911 \begin_inset Argument 1
912 status open
913
914 \begin_layout Plain Layout
915
916 iris-summary
917 \end_layout
918
919 \end_inset
920
921
922 \end_layout
923
924 \begin_layout Plain Layout
925
926 summary(iris[,-5])
927 \end_layout
928
929 \end_inset
930
931
932 \end_layout
933
934 \begin_layout Standard
935 \begin_inset Float table
936 placement document
937 alignment document
938 wide false
939 sideways false
940 status open
941
942 \begin_layout Plain Layout
943 \begin_inset Caption Standard
944
945 \begin_layout Plain Layout
946 Quantiles of the first four variables in the 
947 \family typewriter
948 iris
949 \family default
950  data.
951 \begin_inset CommandInset label
952 LatexCommand label
953 name "tab:xtable-demo"
954
955 \end_inset
956
957
958 \end_layout
959
960 \end_inset
961
962
963 \begin_inset Flex Chunk
964 status open
965
966 \begin_layout Plain Layout
967
968 \begin_inset Argument 1
969 status open
970
971 \begin_layout Plain Layout
972
973 xtable-demo,results=tex,echo=FALSE
974 \end_layout
975
976 \end_inset
977
978
979 \end_layout
980
981 \begin_layout Plain Layout
982
983 if (require('xtable')) {
984 \end_layout
985
986 \begin_layout Plain Layout
987
988 print(xtable(sapply(iris[,-5],
989  quantile)),
990  floating=FALSE)
991 \end_layout
992
993 \begin_layout Plain Layout
994
995 } else cat('the xtable package is not available!
996  please install.packages("xtable")')
997 \end_layout
998
999 \end_inset
1000
1001
1002 \end_layout
1003
1004 \end_inset
1005
1006
1007 \end_layout
1008
1009 \begin_layout Standard
1010 Usually people do not like such 
1011 \begin_inset Quotes eld
1012 \end_inset
1013
1014 raw
1015 \begin_inset Quotes erd
1016 \end_inset
1017
1018  output,
1019  and we can polish these messy numbers with the 
1020 \series bold
1021 xtable
1022 \series default
1023  package.
1024  Table 
1025 \begin_inset CommandInset ref
1026 LatexCommand ref
1027 reference "tab:xtable-demo"
1028 nolink "false"
1029
1030 \end_inset
1031
1032  is an example demonstrating how to use the 
1033 \series bold
1034 xtable
1035 \series default
1036  package with Sweave;
1037  note the chunk option 
1038 \family typewriter
1039 results=tex
1040 \family default
1041  makes sure that the R output is written out as raw \SpecialChar LaTeX
1042  code instead of being wrapped in a special environment (
1043 \family typewriter
1044 Soutput
1045 \family default
1046 ).
1047 \end_layout
1048
1049 \begin_layout Standard
1050 Figure 
1051 \begin_inset CommandInset ref
1052 LatexCommand ref
1053 reference "fig:iris-pairs"
1054 nolink "false"
1055
1056 \end_inset
1057
1058  and 
1059 \begin_inset CommandInset ref
1060 LatexCommand ref
1061 reference "fig:iris-boxplots"
1062 nolink "false"
1063
1064 \end_inset
1065
1066  are two simple examples of producing graphics output with Sweave.
1067 \end_layout
1068
1069 \begin_layout Standard
1070 \begin_inset Float figure
1071 placement document
1072 alignment document
1073 wide false
1074 sideways false
1075 status open
1076
1077 \begin_layout Plain Layout
1078 \align center
1079 \begin_inset Note Note
1080 status open
1081
1082 \begin_layout Plain Layout
1083 Both eps and pdf are set to true so that both latex and pdflatex work.
1084  In practice,
1085  you would probably just choose one.
1086 \end_layout
1087
1088 \end_inset
1089
1090
1091 \end_layout
1092
1093 \begin_layout Plain Layout
1094 \align center
1095 \begin_inset Flex Chunk
1096 status open
1097
1098 \begin_layout Plain Layout
1099
1100 \begin_inset Argument 1
1101 status open
1102
1103 \begin_layout Plain Layout
1104
1105 iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE
1106 \end_layout
1107
1108 \end_inset
1109
1110
1111 \end_layout
1112
1113 \begin_layout Plain Layout
1114
1115 pairs(iris,
1116  col = iris$Species)
1117 \end_layout
1118
1119 \end_inset
1120
1121
1122 \end_layout
1123
1124 \begin_layout Plain Layout
1125 \begin_inset Caption Standard
1126
1127 \begin_layout Plain Layout
1128 Scatter plot matrix of the 
1129 \family typewriter
1130 iris
1131 \family default
1132  data.
1133 \begin_inset CommandInset label
1134 LatexCommand label
1135 name "fig:iris-pairs"
1136
1137 \end_inset
1138
1139
1140 \end_layout
1141
1142 \end_inset
1143
1144
1145 \end_layout
1146
1147 \end_inset
1148
1149
1150 \end_layout
1151
1152 \begin_layout Standard
1153 \begin_inset Float figure
1154 placement document
1155 alignment document
1156 wide false
1157 sideways false
1158 status open
1159
1160 \begin_layout Plain Layout
1161 \align center
1162 \begin_inset Flex Chunk
1163 status open
1164
1165 \begin_layout Plain Layout
1166
1167 \begin_inset Argument 1
1168 status open
1169
1170 \begin_layout Plain Layout
1171
1172 iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T
1173 \end_layout
1174
1175 \end_inset
1176
1177
1178 \end_layout
1179
1180 \begin_layout Plain Layout
1181
1182 par(mar=c(4,5,.1,.3),
1183  las=1)
1184 \end_layout
1185
1186 \begin_layout Plain Layout
1187
1188 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1189 \end_layout
1190
1191 \end_inset
1192
1193
1194 \end_layout
1195
1196 \begin_layout Plain Layout
1197 \begin_inset Caption Standard
1198
1199 \begin_layout Plain Layout
1200 Boxplots of sepal length grouped by species.
1201 \begin_inset CommandInset label
1202 LatexCommand label
1203 name "fig:iris-boxplots"
1204
1205 \end_inset
1206
1207
1208 \end_layout
1209
1210 \end_inset
1211
1212
1213 \end_layout
1214
1215 \end_inset
1216
1217
1218 \end_layout
1219
1220 \begin_layout Standard
1221 We can also export all the R code in an Sweave document as a single R script (
1222 \family sans
1223 File\SpecialChar menuseparator
1224 Export\SpecialChar menuseparator
1225 R/S code
1226 \family default
1227 ),
1228  which is done via 
1229 \family typewriter
1230 R CMD Stangle
1231 \family default
1232 .
1233 \end_layout
1234
1235 \begin_layout Section
1236 The 
1237 \family sans
1238 Sweave
1239 \family default
1240  Module
1241 \end_layout
1242
1243 \begin_layout Standard
1244 The 
1245 \family sans
1246 Sweave
1247 \family default
1248  module declares that a \SpecialChar LyX
1249  document contains literate programming components with R,
1250  so that \SpecialChar LyX
1251  will handle such documents according to the specific converters (see the Customization manual).
1252  The 
1253 \family sans
1254 Sweave
1255 \family default
1256  module uses the 
1257 \family sans
1258 sweave
1259 \family default
1260  converter,
1261  which essentially consists of an R script 
1262 \family sans
1263 lyxsweave.R
1264 \family default
1265  under the 
1266 \family sans
1267 scripts
1268 \family default
1269  directory of \SpecialChar LyX
1270 .
1271  This script takes an 
1272 \family sans
1273 Rnw
1274 \family default
1275  document from \SpecialChar LyX
1276  and call Sweave to process it to a \SpecialChar TeX
1277  document,
1278  then \SpecialChar LyX
1279  takes care of the rest of work.
1280 \end_layout
1281
1282 \begin_layout Standard
1283 The converter from the 
1284 \family sans
1285 sweave
1286 \family default
1287  format to \SpecialChar LaTeX
1288  is a call to R via the command line as:
1289 \end_layout
1290
1291 \begin_layout Standard
1292 \begin_inset ERT
1293 status open
1294
1295 \begin_layout Plain Layout
1296
1297
1298 \backslash
1299 begin{verbatim}
1300 \end_layout
1301
1302 \begin_layout Plain Layout
1303
1304 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e $$r
1305 \end_layout
1306
1307 \begin_layout Plain Layout
1308
1309
1310 \backslash
1311 end{verbatim}
1312 \end_layout
1313
1314 \end_inset
1315
1316
1317 \end_layout
1318
1319 \begin_layout Standard
1320 The \SpecialChar LyX
1321  library directory 
1322 \family typewriter
1323 $$s
1324 \family default
1325  can be found from the menu 
1326 \family sans
1327 Help\SpecialChar menuseparator
1328 About \SpecialChar LyX
1329
1330 \family default
1331 .
1332  All the rest of options passed to R are explained in the R script,
1333  and the \SpecialChar LyX
1334  Customization manual also has detailed explanations about 
1335 \family typewriter
1336 $$i
1337 \family default
1338 ,
1339  
1340 \family typewriter
1341 $$o
1342 \family default
1343 ,
1344  
1345 \family typewriter
1346 $$e
1347 \family default
1348  and 
1349 \family typewriter
1350 $$r
1351 \family default
1352 .
1353  General users do not need to know clearly what they mean,
1354  but here we need to explain a few issues in the R code:
1355 \end_layout
1356
1357 \begin_layout Enumerate
1358 the encoding string of the \SpecialChar LyX
1359  document is passed to R as an command line option;
1360  possible values are 
1361 \family typewriter
1362 ISO-8859-15
1363 \family default
1364  and 
1365 \family typewriter
1366 UTF-8
1367 \family default
1368 ,
1369  etc.
1370  The encoding is used for R to read the 
1371 \family sans
1372 Rnw
1373 \family default
1374  document correctly.
1375 \end_layout
1376
1377 \begin_layout Enumerate
1378 the R code chunks in the Sweave document are executed under the directory of the \SpecialChar LyX
1379  document (if you are not sure,
1380  put 
1381 \family typewriter
1382 getwd()
1383 \family default
1384  in a code chunk to see what is the current working directory).
1385  In this case,
1386  you can put data files under the same directory and use relative paths in R to guarantee reproducibility,
1387  i.e.
1388  we do not need to write hard-coded paths which may not exist in other systems.
1389 \end_layout
1390
1391 \begin_layout Enumerate
1392 if \SpecialChar LaTeX
1393  cannot find 
1394 \family sans
1395 Sweave.sty
1396 \family default
1397 ,
1398  this R script will copy it to the temporary directory automatically where the \SpecialChar LaTeX
1399  compilation takes place.
1400 \end_layout
1401
1402 \begin_layout Enumerate
1403 Sweave will use the filename of the Rnw file as the prefix string for the graphics output by default;
1404  one known issue (
1405 \begin_inset CommandInset href
1406 LatexCommand href
1407 name "#7551"
1408 target "https://www.lyx.org/trac/ticket/7551"
1409 literal "false"
1410
1411 \end_inset
1412
1413 ) is that if the filename (without extension) contains dots,
1414  \SpecialChar LaTeX
1415  can fail to deal with such files in 
1416 \family typewriter
1417
1418 \backslash
1419 includegraphics{}
1420 \family default
1421 .
1422  Therefore,
1423  we have set the default value of the prefix to be the filename with all the dots being replaced by 
1424 \begin_inset Quotes eld
1425 \end_inset
1426
1427 -
1428 \begin_inset Quotes erd
1429 \end_inset
1430
1431 .
1432  We can also solve this problem in Sweave directly:
1433  set the Sweave option 
1434 \family typewriter
1435 prefix.string
1436 \family default
1437  to be a character string without dots,
1438  and this option will override the default value.
1439 \end_layout
1440
1441 \begin_layout Standard
1442 We have pre-specified an option 
1443 \family typewriter
1444 noae
1445 \family default
1446  for the Sweave package.
1447  This option stops Sweave from loading these two packages:
1448 \end_layout
1449
1450 \begin_layout Standard
1451 \begin_inset ERT
1452 status open
1453
1454 \begin_layout Plain Layout
1455
1456
1457 \backslash
1458 begin{verbatim}
1459 \end_layout
1460
1461 \begin_layout Plain Layout
1462
1463
1464 \backslash
1465 RequirePackage[T1]{fontenc}  
1466 \end_layout
1467
1468 \begin_layout Plain Layout
1469
1470
1471 \backslash
1472 RequirePackage{ae}
1473 \end_layout
1474
1475 \begin_layout Plain Layout
1476
1477
1478 \backslash
1479 end{verbatim}
1480 \end_layout
1481
1482 \end_inset
1483
1484
1485 \end_layout
1486
1487 \begin_layout Standard
1488 \SpecialChar LyX
1489  can deal with the font encoding by default,
1490  so there is no need to ask Sweave to do this (furthermore,
1491  this may bring clashes of package options);
1492  besides,
1493  the users can load the 
1494 \series bold
1495 ae
1496 \series default
1497  package by themselves in the preamble if needed.
1498 \end_layout
1499
1500 \begin_layout Section
1501 Trouble Shooting
1502 \end_layout
1503
1504 \begin_layout Standard
1505 Since \SpecialChar LyX
1506  2.0,
1507  we can see the detailed information during compilation via 
1508 \family sans
1509 View\SpecialChar menuseparator
1510 View Messages
1511 \family default
1512 .
1513  This is extremely helpful for trouble shooting – the process of R will be shown in the message panel,
1514  and we will be able to know which chunk goes wrong in cases of errors.
1515  For example,
1516  when you compile this document,
1517  you can see messages like below:
1518 \end_layout
1519
1520 \begin_layout Standard
1521 \begin_inset ERT
1522 status open
1523
1524 \begin_layout Plain Layout
1525
1526
1527 \backslash
1528 begin{verbatim}
1529 \end_layout
1530
1531 \begin_layout Plain Layout
1532
1533 17:58:47.868:
1534  Processing code chunks with options ...
1535 \end_layout
1536
1537 \begin_layout Plain Layout
1538
1539 17:58:47.885:
1540   1 :
1541  echo keep.source term verbatim (label=setup)
1542 \end_layout
1543
1544 \begin_layout Plain Layout
1545
1546 17:58:47.889:
1547   2 :
1548  echo keep.source (label=sweave-manual)
1549 \end_layout
1550
1551 \begin_layout Plain Layout
1552
1553 17:58:47.908:
1554   3 :
1555  echo keep.source term verbatim (label=print-integers)
1556 \end_layout
1557
1558 \begin_layout Plain Layout
1559
1560 ....
1561 \end_layout
1562
1563 \begin_layout Plain Layout
1564
1565 17:58:47.941:
1566  Loading required package:
1567  xtable
1568 \end_layout
1569
1570 \begin_layout Plain Layout
1571
1572 17:58:47.976:
1573   9 :
1574  echo term verbatim pdf (label=iris-pairs)
1575 \end_layout
1576
1577 \begin_layout Plain Layout
1578
1579 ....
1580 \end_layout
1581
1582 \begin_layout Plain Layout
1583
1584 17:58:48.116:
1585  You can now run (pdf)latex on 'sweave.tex'
1586 \end_layout
1587
1588 \begin_layout Plain Layout
1589
1590
1591 \backslash
1592 end{verbatim}
1593 \end_layout
1594
1595 \end_inset
1596
1597
1598 \end_layout
1599
1600 \begin_layout Standard
1601 The string after 
1602 \family typewriter
1603 label=
1604 \family default
1605  is the chunk label.
1606  If any chunk causes an error in R,
1607  you will see the error message here immediately.
1608 \end_layout
1609
1610 \begin_layout Standard
1611 Another issue with the \SpecialChar TeX
1612  code environment is the Sweave code chunk may fail when we change the alignment of the chunk paragraph in \SpecialChar LyX
1613  (using 
1614 \family sans
1615 Paragraph settings
1616 \family default
1617 ),
1618  because \SpecialChar LyX
1619  may add a macro like 
1620 \family typewriter
1621
1622 \backslash
1623 centering{}
1624 \family default
1625  before the code chunk without adding a new line,
1626  which will lead to a code chunk like this:
1627 \end_layout
1628
1629 \begin_layout Standard
1630 \begin_inset ERT
1631 status open
1632
1633 \begin_layout Plain Layout
1634
1635
1636 \backslash
1637 begin{verbatim}
1638 \end_layout
1639
1640 \begin_layout Plain Layout
1641
1642
1643 \backslash
1644 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1645 \end_layout
1646
1647 \begin_layout Plain Layout
1648
1649
1650 \backslash
1651 end{verbatim}
1652 \end_layout
1653
1654 \end_inset
1655
1656
1657 \end_layout
1658
1659 \begin_layout Standard
1660 This is not a legitimate Sweave code chunk,
1661  since 
1662 \family typewriter
1663 <<>>=
1664 \family default
1665  must start in a new line with nothing before it.
1666  This does not happen when using the Chunk inset instead of the \SpecialChar TeX
1667  code environment.
1668  
1669 \end_layout
1670
1671 \begin_layout Section
1672 Resources
1673 \end_layout
1674
1675 \begin_layout Standard
1676 The user manual of Sweave is at 
1677 \begin_inset Flex URL
1678 status collapsed
1679
1680 \begin_layout Plain Layout
1681
1682 https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf
1683 \end_layout
1684
1685 \end_inset
1686
1687 .
1688  The CRAN task view on 
1689 \begin_inset Quotes eld
1690 \end_inset
1691
1692 Reproducible Research
1693 \begin_inset Quotes erd
1694 \end_inset
1695
1696  has a list of Sweave-related packages which can be potentially useful;
1697  see 
1698 \begin_inset Flex URL
1699 status collapsed
1700
1701 \begin_layout Plain Layout
1702
1703 http://cran.r-project.org/web/views/ReproducibleResearch.html
1704 \end_layout
1705
1706 \end_inset
1707
1708 .
1709  Questions about \SpecialChar LyX
1710  and Sweave can be posted to the \SpecialChar LyX
1711  mailing list (
1712 \begin_inset Flex URL
1713 status collapsed
1714
1715 \begin_layout Plain Layout
1716
1717 https://www.lyx.org/MailingLists
1718 \end_layout
1719
1720 \end_inset
1721
1722 ).
1723  More \SpecialChar LyX
1724  examples are provided at 
1725 \begin_inset Flex URL
1726 status collapsed
1727
1728 \begin_layout Plain Layout
1729
1730 https://github.com/yihui/lyx
1731 \end_layout
1732
1733 \end_inset
1734
1735 .
1736 \end_layout
1737
1738 \begin_layout Section
1739 FAQ
1740 \end_layout
1741
1742 \begin_layout Subsection
1743 How to use Sweave with beamer?
1744 \end_layout
1745
1746 \begin_layout Standard
1747 You will end up with \SpecialChar LaTeX
1748  errors if you use Sweave in normal beamer slides in \SpecialChar LyX
1749 ;
1750  the reason is the beamer frame that contains verbatim R code.
1751  The solution is to use a 
1752 \emph on
1753 FragileFrame
1754 \emph default
1755  instead of a normal 
1756 \emph on
1757 Frame
1758 \emph default
1759 ,
1760  or to pass the option 
1761 \family typewriter
1762 fragile
1763 \family default
1764  to the frame title via 
1765 \family sans
1766 Insert\SpecialChar menuseparator
1767 Frame Options
1768 \family default
1769 .
1770 \end_layout
1771
1772 \begin_layout Subsection
1773 Can I define my own R script to compile the Rnw document?
1774 \end_layout
1775
1776 \begin_layout Standard
1777 Yes,
1778  of course.
1779  First you need to understand how the R script 
1780 \family typewriter
1781 $$s
1782 \family sans
1783 /scripts/lyxsweave.R
1784 \family default
1785  works,
1786  which was introduced previously,
1787  and we recommend you to read the R source code as well (on how R takes the paths and uses 
1788 \emph on
1789 Sweave()
1790 \emph default
1791  to handle the Rnw document).
1792  Then you put your customized 
1793 \family sans
1794 lyxsweave.R
1795 \family default
1796  under the 
1797 \family sans
1798 scripts
1799 \family default
1800  directory of your 
1801 \emph on
1802 user directory
1803 \emph default
1804  (again,
1805  see 
1806 \family sans
1807 Help\SpecialChar menuseparator
1808 About \SpecialChar LyX
1809
1810 \family default
1811 ).
1812  Note \SpecialChar LyX
1813  will check the 
1814 \emph on
1815 user directory
1816 \emph default
1817  before going to its own 
1818 \emph on
1819 library directory
1820 \emph default
1821 ;
1822  if an R script 
1823 \family sans
1824 lyxsweave.R
1825 \family default
1826  is found in the user directory,
1827  it will be used.
1828  It is not recommended to hack the script under the \SpecialChar LyX
1829  library directory directly,
1830  since updating \SpecialChar LyX
1831  will override you modified script (the user directory will not be affected).
1832 \end_layout
1833
1834 \begin_layout Standard
1835 Beside the support for the official Sweave in R,
1836  the 
1837 \series bold
1838 knitr
1839 \series default
1840  package is also supported,
1841  which is an alternative tool to Sweave;
1842  the corresponding module is named 
1843 \family sans
1844 Rnw (knitr)
1845 \family default
1846 .
1847 \end_layout
1848
1849 \end_body
1850 \end_document