]> git.lyx.org Git - lyx.git/blob - lib/examples/sweave.lyx
6d758fa4626e7728cfe89c0a02c7091ee158dcb2
[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 ERT
895 status open
896
897 \begin_layout Plain Layout
898
899 <<iris-pairs,fig=TRUE,width=4.5,height=4.5>>=
900 \end_layout
901
902 \begin_layout Plain Layout
903
904 pairs(iris, col = iris$Species)
905 \end_layout
906
907 \begin_layout Plain Layout
908
909 @
910 \end_layout
911
912 \end_inset
913
914
915 \end_layout
916
917 \begin_layout Plain Layout
918 \begin_inset Caption Standard
919
920 \begin_layout Plain Layout
921 Scatter plot matrix of the 
922 \family typewriter
923 iris
924 \family default
925  data.
926 \begin_inset CommandInset label
927 LatexCommand label
928 name "fig:iris-pairs"
929
930 \end_inset
931
932
933 \end_layout
934
935 \end_inset
936
937
938 \end_layout
939
940 \end_inset
941
942
943 \end_layout
944
945 \begin_layout Standard
946 \begin_inset Float figure
947 wide false
948 sideways false
949 status open
950
951 \begin_layout Plain Layout
952 \align center
953 \begin_inset ERT
954 status open
955
956 \begin_layout Plain Layout
957
958 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F>>=
959 \end_layout
960
961 \begin_layout Plain Layout
962
963 par(mar=c(4,5,.1,.3), las=1)
964 \end_layout
965
966 \begin_layout Plain Layout
967
968 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
969 \end_layout
970
971 \begin_layout Plain Layout
972
973 @
974 \end_layout
975
976 \end_inset
977
978
979 \end_layout
980
981 \begin_layout Plain Layout
982 \begin_inset Caption Standard
983
984 \begin_layout Plain Layout
985 Boxplots of sepal length grouped by species.
986 \begin_inset CommandInset label
987 LatexCommand label
988 name "fig:iris-boxplots"
989
990 \end_inset
991
992
993 \end_layout
994
995 \end_inset
996
997
998 \end_layout
999
1000 \end_inset
1001
1002
1003 \end_layout
1004
1005 \begin_layout Standard
1006 We can also export all the R code in an Sweave document as a single R script
1007  (
1008 \family sans
1009 File\SpecialChar \menuseparator
1010 Export\SpecialChar \menuseparator
1011 R/S code
1012 \family default
1013 ), which is done via 
1014 \family typewriter
1015 R CMD Stangle
1016 \family default
1017 .
1018 \end_layout
1019
1020 \begin_layout Section
1021 The 
1022 \family sans
1023 Sweave
1024 \family default
1025  Module
1026 \end_layout
1027
1028 \begin_layout Standard
1029 The 
1030 \family sans
1031 Sweave
1032 \family default
1033  module declares that a LyX document contains literate programming components
1034  with R, so that LyX will handle such documents according to the specific
1035  converters (see the Customization manual).
1036  The 
1037 \family sans
1038 Sweave
1039 \family default
1040  module uses the 
1041 \family sans
1042 sweave
1043 \family default
1044  converter, which essentially consists of an R script 
1045 \family sans
1046 lyxsweave.R
1047 \family default
1048  under the 
1049 \family sans
1050 scripts
1051 \family default
1052  directory of LyX.
1053  This script takes an 
1054 \family sans
1055 Rnw
1056 \family default
1057  document from LyX and call Sweave to process it to a TeX document, then
1058  LyX takes care of the rest of work.
1059 \end_layout
1060
1061 \begin_layout Standard
1062 The converter from the 
1063 \family sans
1064 sweave
1065 \family default
1066  format to LaTeX is a call to R via the command line as:
1067 \end_layout
1068
1069 \begin_layout Standard
1070 \begin_inset ERT
1071 status open
1072
1073 \begin_layout Plain Layout
1074
1075
1076 \backslash
1077 begin{verbatim}
1078 \end_layout
1079
1080 \begin_layout Plain Layout
1081
1082 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1083  $$r
1084 \end_layout
1085
1086 \begin_layout Plain Layout
1087
1088
1089 \backslash
1090 end{verbatim}
1091 \end_layout
1092
1093 \end_inset
1094
1095
1096 \end_layout
1097
1098 \begin_layout Standard
1099 The LyX library directory 
1100 \family typewriter
1101 $$s
1102 \family default
1103  can be found from the menu 
1104 \family sans
1105 Help\SpecialChar \menuseparator
1106 About LyX
1107 \family default
1108 .
1109  All the rest of options passed to R are explained in the R script, and
1110  the LyX Customization manual also has detailed explanations about 
1111 \family typewriter
1112 $$i
1113 \family default
1114
1115 \family typewriter
1116 $$o
1117 \family default
1118
1119 \family typewriter
1120 $$e
1121 \family default
1122  and 
1123 \family typewriter
1124 $$r
1125 \family default
1126 .
1127  General users do not need to know clearly what they mean, but here we need
1128  to explain a few issues in the R code:
1129 \end_layout
1130
1131 \begin_layout Enumerate
1132 the encoding string of the LyX document is passed to R as an command line
1133  option; possible values are 
1134 \family typewriter
1135 ISO-8859-15
1136 \family default
1137  and 
1138 \family typewriter
1139 UTF-8
1140 \family default
1141 , etc.
1142  The encoding is used for R to read the 
1143 \family sans
1144 Rnw
1145 \family default
1146  document correctly.
1147 \end_layout
1148
1149 \begin_layout Enumerate
1150 the R code chunks in the Sweave document are executed under the directory
1151  of the LyX document (if you are not sure, put 
1152 \family typewriter
1153 getwd()
1154 \family default
1155  in a code chunk to see what is the current working directory).
1156  In this case, you can put data files under the same directory and use relative
1157  paths in R to guarantee reproducibility, i.e.
1158  we do not need to write hard-coded paths which may not exist in other systems.
1159 \end_layout
1160
1161 \begin_layout Enumerate
1162 if LaTeX cannot find 
1163 \family sans
1164 Sweave.sty
1165 \family default
1166 , this R script will copy it to the temporary directory automatically where
1167  the LaTeX compilation takes place.
1168 \end_layout
1169
1170 \begin_layout Enumerate
1171 Sweave will use the filename of the Rnw file as the prefix string for the
1172  graphics output by default; one known issue (
1173 \begin_inset CommandInset href
1174 LatexCommand href
1175 name "#7551"
1176 target "http://www.lyx.org/trac/ticket/7551"
1177
1178 \end_inset
1179
1180 ) is that if the filename (without extension) contains dots, LaTeX can fail
1181  to deal with such files in 
1182 \family typewriter
1183
1184 \backslash
1185 includegraphics{}
1186 \family default
1187 .
1188  Therefore, we have set the default value of the prefix to be the filename
1189  with all the dots being replaced by 
1190 \begin_inset Quotes eld
1191 \end_inset
1192
1193 -
1194 \begin_inset Quotes erd
1195 \end_inset
1196
1197 .
1198  We can also solve this problem in Sweave directly: set the Sweave option
1199  
1200 \family typewriter
1201 prefix.string
1202 \family default
1203  to be a character string without dots, and this option will override the
1204  default value.
1205 \end_layout
1206
1207 \begin_layout Standard
1208 We have pre-specified an option 
1209 \family typewriter
1210 noae
1211 \family default
1212  for the Sweave package.
1213  This option stops Sweave from loading these two packages:
1214 \end_layout
1215
1216 \begin_layout Standard
1217 \begin_inset ERT
1218 status open
1219
1220 \begin_layout Plain Layout
1221
1222
1223 \backslash
1224 begin{verbatim}
1225 \end_layout
1226
1227 \begin_layout Plain Layout
1228
1229
1230 \backslash
1231 RequirePackage[T1]{fontenc}  
1232 \end_layout
1233
1234 \begin_layout Plain Layout
1235
1236
1237 \backslash
1238 RequirePackage{ae}
1239 \end_layout
1240
1241 \begin_layout Plain Layout
1242
1243
1244 \backslash
1245 end{verbatim}
1246 \end_layout
1247
1248 \end_inset
1249
1250
1251 \end_layout
1252
1253 \begin_layout Standard
1254 LyX can deal with the font encoding by default, so there is no need to ask
1255  Sweave to do this (furthermore, this may bring clashes of package options);
1256  besides, the users can load the 
1257 \series bold
1258 ae
1259 \series default
1260  package by themselves in the preamble if needed.
1261 \end_layout
1262
1263 \begin_layout Section
1264 Trouble Shooting
1265 \end_layout
1266
1267 \begin_layout Standard
1268 Since LyX 2.0, we can see the detailed information during compilation via
1269  
1270 \family sans
1271 View\SpecialChar \menuseparator
1272 View Messages
1273 \family default
1274 .
1275  This is extremely helpful for trouble shooting -- the process of R will
1276  be shown in the message panel, and we will be able to know which chunk
1277  goes wrong in cases of errors.
1278  For example, when you compile this document, you can see messages like
1279  below:
1280 \end_layout
1281
1282 \begin_layout Standard
1283 \begin_inset ERT
1284 status open
1285
1286 \begin_layout Plain Layout
1287
1288
1289 \backslash
1290 begin{verbatim}
1291 \end_layout
1292
1293 \begin_layout Plain Layout
1294
1295 17:58:47.868: Processing code chunks with options ...
1296 \end_layout
1297
1298 \begin_layout Plain Layout
1299
1300 17:58:47.885:  1 : echo keep.source term verbatim (label=setup)
1301 \end_layout
1302
1303 \begin_layout Plain Layout
1304
1305 17:58:47.889:  2 : echo keep.source (label=sweave-manual)
1306 \end_layout
1307
1308 \begin_layout Plain Layout
1309
1310 17:58:47.908:  3 : echo keep.source term verbatim (label=print-integers)
1311 \end_layout
1312
1313 \begin_layout Plain Layout
1314
1315 ....
1316 \end_layout
1317
1318 \begin_layout Plain Layout
1319
1320 17:58:47.941: Loading required package: xtable
1321 \end_layout
1322
1323 \begin_layout Plain Layout
1324
1325 17:58:47.976:  9 : echo term verbatim pdf (label=iris-pairs)
1326 \end_layout
1327
1328 \begin_layout Plain Layout
1329
1330 ....
1331 \end_layout
1332
1333 \begin_layout Plain Layout
1334
1335 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1336 \end_layout
1337
1338 \begin_layout Plain Layout
1339
1340
1341 \backslash
1342 end{verbatim}
1343 \end_layout
1344
1345 \end_inset
1346
1347
1348 \end_layout
1349
1350 \begin_layout Standard
1351 The string after 
1352 \family typewriter
1353 label=
1354 \family default
1355  is the chunk label.
1356  If any chunk causes an error in R, you will see the error message here
1357  immediately.
1358 \end_layout
1359
1360 \begin_layout Standard
1361 Another known issue is the Sweave code chunk may fail when we change the
1362  alignment of the chunk paragraph in LyX (using 
1363 \family sans
1364 Paragraph settings
1365 \family default
1366 ), because LyX may add a macro like 
1367 \family typewriter
1368
1369 \backslash
1370 centering{}
1371 \family default
1372  before the code chunk without adding a new line, which will lead to a code
1373  chunk like this:
1374 \end_layout
1375
1376 \begin_layout Standard
1377 \begin_inset ERT
1378 status open
1379
1380 \begin_layout Plain Layout
1381
1382
1383 \backslash
1384 begin{verbatim}
1385 \end_layout
1386
1387 \begin_layout Plain Layout
1388
1389
1390 \backslash
1391 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1392 \end_layout
1393
1394 \begin_layout Plain Layout
1395
1396
1397 \backslash
1398 end{verbatim}
1399 \end_layout
1400
1401 \end_inset
1402
1403
1404 \end_layout
1405
1406 \begin_layout Standard
1407 This is not a legitimate Sweave code chunk, since 
1408 \family typewriter
1409 <<>>=
1410 \family default
1411  must start in a new line with nothing before it.
1412  Therefore we recommend you to double check the real source code via 
1413 \family sans
1414 View\SpecialChar \menuseparator
1415 View Source
1416 \family default
1417  when changing the alignment of a chunk paragraph, and make sure 
1418 \family typewriter
1419 <<>>=
1420 \family default
1421  appears in a new line.
1422  This explains why we manually added an empty line in the code chunk in
1423  Table 
1424 \begin_inset CommandInset ref
1425 LatexCommand ref
1426 reference "tab:xtable-demo"
1427
1428 \end_inset
1429
1430 .
1431 \end_layout
1432
1433 \begin_layout Section
1434 Resources
1435 \end_layout
1436
1437 \begin_layout Standard
1438 The homepage of Sweave is at 
1439 \begin_inset Flex URL
1440 status collapsed
1441
1442 \begin_layout Plain Layout
1443
1444 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1445 \end_layout
1446
1447 \end_inset
1448
1449 .
1450  The CRAN task view on 
1451 \begin_inset Quotes eld
1452 \end_inset
1453
1454 Reproducible Research
1455 \begin_inset Quotes erd
1456 \end_inset
1457
1458  has a list of Sweave-related packages which can be potentially useful;
1459  see 
1460 \begin_inset Flex URL
1461 status collapsed
1462
1463 \begin_layout Plain Layout
1464
1465 http://cran.r-project.org/web/views/ReproducibleResearch.html
1466 \end_layout
1467
1468 \end_inset
1469
1470 .
1471  Questions about LyX and Sweave can be posted to the LyX mailing list (
1472 \begin_inset Flex URL
1473 status collapsed
1474
1475 \begin_layout Plain Layout
1476
1477 http://www.lyx.org/MailingLists
1478 \end_layout
1479
1480 \end_inset
1481
1482 ).
1483  More LyX examples are provided at 
1484 \begin_inset Flex URL
1485 status collapsed
1486
1487 \begin_layout Plain Layout
1488
1489 https://github.com/yihui/lyx
1490 \end_layout
1491
1492 \end_inset
1493
1494 .
1495 \end_layout
1496
1497 \begin_layout Section
1498 FAQ
1499 \end_layout
1500
1501 \begin_layout Subsection
1502 How to use Sweave with beamer?
1503 \end_layout
1504
1505 \begin_layout Standard
1506 You will end up with LaTeX errors if you use Sweave in normal beamer slides
1507  in LyX; the reason is the beamer frame that contains verbatim R code.
1508  The solution is to use a 
1509 \emph on
1510 FragileFrame
1511 \emph default
1512  instead of a normal 
1513 \emph on
1514 Frame
1515 \emph default
1516 , or to pass the option 
1517 \family typewriter
1518 fragile
1519 \family default
1520  to the frame title via 
1521 \family sans
1522 Insert\SpecialChar \menuseparator
1523 Frame Options
1524 \family default
1525 .
1526 \end_layout
1527
1528 \begin_layout Subsection
1529 Can I define my own R script to compile the Rnw document?
1530 \end_layout
1531
1532 \begin_layout Standard
1533 Yes, of course.
1534  First you need to understand how the R script 
1535 \family typewriter
1536 $$s
1537 \family sans
1538 /scripts/lyxsweave.R
1539 \family default
1540  works, which was introduced previously, and we recommend you to read the
1541  R source code as well (on how R takes the paths and uses 
1542 \emph on
1543 Sweave()
1544 \emph default
1545  to handle the Rnw document).
1546  Then you put your customized 
1547 \family sans
1548 lyxsweave.R
1549 \family default
1550  under the 
1551 \family sans
1552 scripts
1553 \family default
1554  directory of your 
1555 \emph on
1556 user directory
1557 \emph default
1558  (again, see 
1559 \family sans
1560 Help\SpecialChar \menuseparator
1561 About LyX
1562 \family default
1563 ).
1564  Note LyX will check the 
1565 \emph on
1566 user directory
1567 \emph default
1568  before going to its own 
1569 \emph on
1570 library directory
1571 \emph default
1572 ; if an R script 
1573 \family sans
1574 lyxsweave.R
1575 \family default
1576  is found in the user directory, it will be used.
1577  It is not recommended to hack the script under the LyX library directory
1578  directly, since updating LyX will override you modified script (the user
1579  directory will not be affected).
1580 \end_layout
1581
1582 \begin_layout Standard
1583 Beside the support for the official Sweave in R, the 
1584 \series bold
1585 knitr
1586 \series default
1587  package is also supported, which is an alternative tool to Sweave; the
1588  corresponding module is named 
1589 \family sans
1590 Rnw (knitr)
1591 \family default
1592 .
1593 \end_layout
1594
1595 \end_body
1596 \end_document