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