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