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