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