]> git.lyx.org Git - lyx.git/blob - lib/examples/sweave.lyx
port today's changes to trunk
[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 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 (we recommend you to learn 
279 \family sans
280 PATH
281 \family default
282  anyway).
283 \end_layout
284
285 \begin_layout Standard
286 If 
287 \family typewriter
288 Rscript
289 \family default
290  is not present in 
291 \family sans
292 PATH
293 \family default
294 , the 
295 \family sans
296 Sweave
297 \family default
298  module will be unavailable.
299  It is recommended to use the latest version of R, since Sweave is still
300  being updated.
301  Another dependency is introduced by the LaTeX style file 
302 \family sans
303 Sweave.sty
304 \family default
305  which is shipped with R under this path (it is not on CTAN):
306 \end_layout
307
308 \begin_layout Standard
309 \begin_inset ERT
310 status open
311
312 \begin_layout Plain Layout
313
314 <<sweave-sty,eval=FALSE>>=
315 \end_layout
316
317 \begin_layout Plain Layout
318
319 file.path(R.home('share'), 'texmf', 'tex', 'latex')
320 \end_layout
321
322 \begin_layout Plain Layout
323
324 @
325 \end_layout
326
327 \end_inset
328
329
330 \end_layout
331
332 \begin_layout Standard
333 MikTeX users may add the texmf directory to the list of root directories
334  in MikTeX through MikTeX settings.
335  Further note that 
336 \family sans
337 Sweave.sty
338 \family default
339  depends on the 
340 \series bold
341 fancyvrb
342 \series default
343  package and 
344 \emph on
345 may
346 \emph default
347  depend on 
348 \series bold
349 ae
350 \series default
351 .
352  If LyX cannot find 
353 \family sans
354 Sweave.sty
355 \family default
356 , it will directly copy from R, so the Sweave module works even if you do
357  not know where is this style file.
358 \end_layout
359
360 \begin_layout Section
361 Sweave Examples
362 \end_layout
363
364 \begin_layout Standard
365 Before we write an Sweave document, we can set some Sweave options; the
366  command is 
367 \family typewriter
368
369 \backslash
370 SweaveOpts{}
371 \family default
372  in Sweave and it can be inserted in a LyX document by 
373 \family sans
374 Insert\SpecialChar \menuseparator
375 Custom Insets\SpecialChar \menuseparator
376 Sweave Options
377 \family default
378 , or just use TeX code to write it (
379 \family sans
380 Insert\SpecialChar \menuseparator
381 TeX Code
382 \family default
383 ).
384  For example, we can set a global option 
385 \family typewriter
386 echo=FALSE
387 \family default
388  in the beginning of a document to suppress all R code (by default, both
389  R code and its output will show up in the LaTeX document).
390  
391 \end_layout
392
393 \begin_layout Standard
394 To write R code in LyX, you can first switch to the 
395 \family typewriter
396 Chunk
397 \family default
398  environment in the layout styles list (droplist in the top-left corner
399  in the toolbar), or you can simply open a TeX code environment and write
400  Sweave code chunks in it.
401  The latter approach is recommended since it is more stable.
402 \end_layout
403
404 \begin_layout Standard
405 Some R options can be useful to the Sweave output as well, e.g.
406  
407 \family typewriter
408 width
409 \family default
410  and 
411 \family typewriter
412 useFancyQuotes
413 \family default
414 .
415  The former option can adjust the width of the output, and the latter can
416  avoid a common problem with quotes.
417  Users are encouraged to read the Sweave manual in R for more details.
418 \end_layout
419
420 \begin_layout Standard
421 \begin_inset ERT
422 status open
423
424 \begin_layout Plain Layout
425
426 <<setup, keep.source=TRUE>>=
427 \end_layout
428
429 \begin_layout Plain Layout
430
431 ## the option 'digits' can set the (approximate) number of digits
432 \end_layout
433
434 \begin_layout Plain Layout
435
436 options(width = 70, useFancyQuotes = FALSE, digits = 4)
437 \end_layout
438
439 \begin_layout Plain Layout
440
441 @
442 \end_layout
443
444 \begin_layout Plain Layout
445
446 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
447 \end_layout
448
449 \begin_layout Plain Layout
450
451 ## view the Sweave vignette (requires R >= 2.13.0)
452 \end_layout
453
454 \begin_layout Plain Layout
455
456 vignette("Sweave", package = "utils")
457 \end_layout
458
459 \begin_layout Plain Layout
460
461 @
462 \end_layout
463
464 \end_inset
465
466
467 \end_layout
468
469 \begin_layout Standard
470 A simple example that will run in any S engine: 
471 \end_layout
472
473 \begin_layout Standard
474 \begin_inset ERT
475 status open
476
477 \begin_layout Plain Layout
478
479 <<print-integers, keep.source=TRUE>>=
480 \end_layout
481
482 \begin_layout Plain Layout
483
484 ## the integers from 1 to 10 are
485 \end_layout
486
487 \begin_layout Plain Layout
488
489 1:10 
490 \end_layout
491
492 \begin_layout Plain Layout
493
494 @
495 \end_layout
496
497 \begin_layout Plain Layout
498
499 <<hide-results, results=hide>>=
500 \end_layout
501
502 \begin_layout Plain Layout
503
504 ## intentionally hide the output by setting results=hide
505 \end_layout
506
507 \begin_layout Plain Layout
508
509 print(1:20)
510 \end_layout
511
512 \begin_layout Plain Layout
513
514 @
515 \end_layout
516
517 \end_inset
518
519
520 \end_layout
521
522 \begin_layout Standard
523 We can also emulate a simple calculator: 
524 \end_layout
525
526 \begin_layout Standard
527 \begin_inset ERT
528 status open
529
530 \begin_layout Plain Layout
531
532 <<calculator>>=
533 \end_layout
534
535 \begin_layout Plain Layout
536
537 1 + pi
538 \end_layout
539
540 \begin_layout Plain Layout
541
542 sin(pi/6)
543 \end_layout
544
545 \begin_layout Plain Layout
546
547 @
548 \end_layout
549
550 \end_inset
551
552
553 \end_layout
554
555 \begin_layout Standard
556 Now we look at some Gaussian data:
557 \end_layout
558
559 \begin_layout Standard
560 \begin_inset ERT
561 status open
562
563 \begin_layout Plain Layout
564
565 <<rnorm>>=
566 \end_layout
567
568 \begin_layout Plain Layout
569
570 set.seed(123)  # for reproducibility
571 \end_layout
572
573 \begin_layout Plain Layout
574
575 x <- rnorm(20)
576 \end_layout
577
578 \begin_layout Plain Layout
579
580 print(x)
581 \end_layout
582
583 \begin_layout Plain Layout
584
585 t1 <- t.test(x)
586 \end_layout
587
588 \begin_layout Plain Layout
589
590 @
591 \end_layout
592
593 \end_inset
594
595
596 \end_layout
597
598 \begin_layout Standard
599 Note that we can easily integrate some numbers into standard text; this
600  is done with the command 
601 \family typewriter
602
603 \backslash
604 Sexpr{}
605 \family default
606 , and the corresponding LyX menu is 
607 \family sans
608 Insert\SpecialChar \menuseparator
609 Custom Insets\SpecialChar \menuseparator
610 S/R expression
611 \family default
612 , or alternatively just use TeX code.
613  For example, the third element of the vector 
614 \family typewriter
615 x
616 \family default
617  is 
618 \begin_inset Flex S/R expression
619 status collapsed
620
621 \begin_layout Plain Layout
622
623 x[3]
624 \end_layout
625
626 \end_inset
627
628  (i.e.
629  
630 \family typewriter
631 x[3]
632 \family default
633 ) and the 
634 \begin_inset Formula $p$
635 \end_inset
636
637 -value of the test is 
638 \begin_inset Flex S/R expression
639 status collapsed
640
641 \begin_layout Plain Layout
642
643 format.pval(t1$p.value)
644 \end_layout
645
646 \end_inset
647
648 .
649  You can round these numbers using functions like 
650 \emph on
651 round()
652 \emph default
653  in R.
654  
655 \end_layout
656
657 \begin_layout Standard
658
659 \family roman
660 \series medium
661 \shape up
662 \size normal
663 \emph off
664 \bar no
665 \noun off
666 \color none
667 Now we look at a summary of the 
668 \family typewriter
669 \series default
670 \shape default
671 \size default
672 \emph default
673 \bar default
674 \noun default
675 \color inherit
676 iris
677 \family roman
678 \series medium
679 \shape up
680 \size normal
681 \emph off
682 \bar no
683 \noun off
684 \color none
685  dataset:
686 \end_layout
687
688 \begin_layout Standard
689 \begin_inset ERT
690 status open
691
692 \begin_layout Plain Layout
693
694 <<iris-summary>>=
695 \end_layout
696
697 \begin_layout Plain Layout
698
699 summary(iris[,-5])
700 \end_layout
701
702 \begin_layout Plain Layout
703
704 @
705 \end_layout
706
707 \end_inset
708
709
710 \end_layout
711
712 \begin_layout Standard
713 \begin_inset Float table
714 wide false
715 sideways false
716 status open
717
718 \begin_layout Plain Layout
719 \begin_inset Caption
720
721 \begin_layout Plain Layout
722 Quantiles of the first four variables in the 
723 \family typewriter
724 iris
725 \family default
726  data.
727 \begin_inset CommandInset label
728 LatexCommand label
729 name "tab:xtable-demo"
730
731 \end_inset
732
733
734 \end_layout
735
736 \end_inset
737
738
739 \end_layout
740
741 \begin_layout Plain Layout
742 \begin_inset ERT
743 status open
744
745 \begin_layout Plain Layout
746
747
748 \backslash
749 begin{center}
750 \end_layout
751
752 \begin_layout Plain Layout
753
754 <<xtable-demo,results=tex,echo=FALSE>>=
755 \end_layout
756
757 \begin_layout Plain Layout
758
759 if (require('xtable')) {
760 \end_layout
761
762 \begin_layout Plain Layout
763
764 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
765 \end_layout
766
767 \begin_layout Plain Layout
768
769 } else cat('the xtable package is not available! please install.packages("xtable"
770 )')
771 \end_layout
772
773 \begin_layout Plain Layout
774
775 @
776 \end_layout
777
778 \begin_layout Plain Layout
779
780
781 \backslash
782 end{center}
783 \end_layout
784
785 \end_inset
786
787
788 \end_layout
789
790 \end_inset
791
792
793 \end_layout
794
795 \begin_layout Standard
796 Usually people do not like such 
797 \begin_inset Quotes eld
798 \end_inset
799
800 raw
801 \begin_inset Quotes erd
802 \end_inset
803
804  output, and we can polish these messy numbers with the 
805 \series bold
806 xtable
807 \series default
808  package.
809  Table 
810 \begin_inset CommandInset ref
811 LatexCommand ref
812 reference "tab:xtable-demo"
813
814 \end_inset
815
816  is an example demonstrating how to use the 
817 \series bold
818 xtable
819 \series default
820  package with Sweave; note the chunk option 
821 \family typewriter
822 results=tex
823 \family default
824  makes sure that the R output is written out as raw LaTeX code instead of
825  being wrapped in a special environment (
826 \family typewriter
827 Soutput
828 \family default
829 ).
830 \end_layout
831
832 \begin_layout Standard
833 Figure 
834 \begin_inset CommandInset ref
835 LatexCommand ref
836 reference "fig:iris-pairs"
837
838 \end_inset
839
840  and 
841 \begin_inset CommandInset ref
842 LatexCommand ref
843 reference "fig:iris-boxplots"
844
845 \end_inset
846
847  are two simple examples of producing graphics output with Sweave.
848 \end_layout
849
850 \begin_layout Standard
851 \begin_inset Float figure
852 wide false
853 sideways false
854 status open
855
856 \begin_layout Plain Layout
857 \begin_inset ERT
858 status open
859
860 \begin_layout Plain Layout
861
862
863 \backslash
864 begin{center}
865 \end_layout
866
867 \begin_layout Plain Layout
868
869 <<iris-pairs,fig=TRUE,width=4.5,height=4.5>>=
870 \end_layout
871
872 \begin_layout Plain Layout
873
874 pairs(iris, col = iris$Species)
875 \end_layout
876
877 \begin_layout Plain Layout
878
879 @
880 \end_layout
881
882 \begin_layout Plain Layout
883
884
885 \backslash
886 end{center}
887 \end_layout
888
889 \end_inset
890
891
892 \end_layout
893
894 \begin_layout Plain Layout
895 \begin_inset Caption
896
897 \begin_layout Plain Layout
898 Scatter plot matrix of the 
899 \family typewriter
900 iris
901 \family default
902  data.
903 \begin_inset CommandInset label
904 LatexCommand label
905 name "fig:iris-pairs"
906
907 \end_inset
908
909
910 \end_layout
911
912 \end_inset
913
914
915 \end_layout
916
917 \end_inset
918
919
920 \end_layout
921
922 \begin_layout Standard
923 \begin_inset Float figure
924 wide false
925 sideways false
926 status open
927
928 \begin_layout Plain Layout
929 \begin_inset ERT
930 status open
931
932 \begin_layout Plain Layout
933
934
935 \backslash
936 begin{center}
937 \end_layout
938
939 \begin_layout Plain Layout
940
941 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F>>=
942 \end_layout
943
944 \begin_layout Plain Layout
945
946 par(mar=c(4,5,.1,.3), las=1)
947 \end_layout
948
949 \begin_layout Plain Layout
950
951 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
952 \end_layout
953
954 \begin_layout Plain Layout
955
956 @
957 \end_layout
958
959 \begin_layout Plain Layout
960
961
962 \backslash
963 end{center}
964 \end_layout
965
966 \end_inset
967
968
969 \end_layout
970
971 \begin_layout Plain Layout
972 \begin_inset Caption
973
974 \begin_layout Plain Layout
975 Boxplots of sepal length grouped by species.
976 \begin_inset CommandInset label
977 LatexCommand label
978 name "fig:iris-boxplots"
979
980 \end_inset
981
982
983 \end_layout
984
985 \end_inset
986
987
988 \end_layout
989
990 \end_inset
991
992
993 \end_layout
994
995 \begin_layout Standard
996 We can also export all the R code in an Sweave document as a single R script
997  (
998 \family sans
999 File\SpecialChar \menuseparator
1000 Export\SpecialChar \menuseparator
1001 R/S code
1002 \family default
1003 ), which is done via 
1004 \family typewriter
1005 R CMD Stangle
1006 \family default
1007 .
1008 \end_layout
1009
1010 \begin_layout Section
1011 The 
1012 \family sans
1013 Sweave
1014 \family default
1015  Module
1016 \end_layout
1017
1018 \begin_layout Standard
1019 The 
1020 \family sans
1021 Sweave
1022 \family default
1023  module declares that a LyX document contains literate programming components
1024  with R, so that LyX will handle such documents according to the specific
1025  converters (see the Customization manual).
1026  The 
1027 \family sans
1028 Sweave
1029 \family default
1030  module uses the 
1031 \family sans
1032 sweave
1033 \family default
1034  converter, which essentially consists of an R script 
1035 \family sans
1036 lyxsweave.R
1037 \family default
1038  under the 
1039 \family sans
1040 scripts
1041 \family default
1042  directory of LyX.
1043  This script takes an 
1044 \family sans
1045 Rnw
1046 \family default
1047  document from LyX and call Sweave to process it to a TeX document, then
1048  LyX takes care of the rest of work.
1049 \end_layout
1050
1051 \begin_layout Standard
1052 The converter from the 
1053 \family sans
1054 sweave
1055 \family default
1056  format to LaTeX is a call to R via the command line as:
1057 \end_layout
1058
1059 \begin_layout Standard
1060 \begin_inset ERT
1061 status open
1062
1063 \begin_layout Plain Layout
1064
1065
1066 \backslash
1067 begin{verbatim}
1068 \end_layout
1069
1070 \begin_layout Plain Layout
1071
1072 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1073  $$r
1074 \end_layout
1075
1076 \begin_layout Plain Layout
1077
1078
1079 \backslash
1080 end{verbatim}
1081 \end_layout
1082
1083 \end_inset
1084
1085
1086 \end_layout
1087
1088 \begin_layout Standard
1089 The LyX library directory 
1090 \family typewriter
1091 $$s
1092 \family default
1093  can be found from the menu 
1094 \family sans
1095 Help\SpecialChar \menuseparator
1096 About LyX
1097 \family default
1098 .
1099  All the rest of options passed to R are explained in the R script, and
1100  the LyX Customization manual also has detailed explanations about 
1101 \family typewriter
1102 $$i
1103 \family default
1104
1105 \family typewriter
1106 $$o
1107 \family default
1108
1109 \family typewriter
1110 $$e
1111 \family default
1112  and 
1113 \family typewriter
1114 $$r
1115 \family default
1116 .
1117  General users do not need to know clearly what they mean, but here we need
1118  to explain a few issues in the R code:
1119 \end_layout
1120
1121 \begin_layout Enumerate
1122 the encoding string of the LyX document is passed to R as an command line
1123  option; possible values are 
1124 \family typewriter
1125 ISO-8859-15
1126 \family default
1127  and 
1128 \family typewriter
1129 UTF-8
1130 \family default
1131 , etc.
1132  The encoding is used for R to read the 
1133 \family sans
1134 Rnw
1135 \family default
1136  document correctly.
1137 \end_layout
1138
1139 \begin_layout Enumerate
1140 the R code chunks in the Sweave document are executed under the directory
1141  of the LyX document (if you are not sure, put 
1142 \family typewriter
1143 getwd()
1144 \family default
1145  in a code chunk to see what is the current working directory).
1146  In this case, you can put data files under the same directory and use relative
1147  paths in R to guarantee reproducibility, i.e.
1148  we do not need to write hard-coded paths which may not exist in other systems.
1149 \end_layout
1150
1151 \begin_layout Enumerate
1152 if LaTeX cannot find 
1153 \family sans
1154 Sweave.sty
1155 \family default
1156 , this R script will copy it to the temporary directory automatically where
1157  the LaTeX compilation takes place.
1158 \end_layout
1159
1160 \begin_layout Enumerate
1161 Sweave will use the filename of the Rnw file as the prefix string for the
1162  graphics output by default; one known issue (
1163 \begin_inset CommandInset href
1164 LatexCommand href
1165 name "#7551"
1166 target "http://www.lyx.org/trac/ticket/7551"
1167
1168 \end_inset
1169
1170 ) is that if the filename (without extension) contains dots, LaTeX can fail
1171  to deal with such files in 
1172 \family typewriter
1173
1174 \backslash
1175 includegraphics{}
1176 \family default
1177 .
1178  Therefore, we have set the default value of the prefix to be the filename
1179  with all the dots being replaced by 
1180 \begin_inset Quotes eld
1181 \end_inset
1182
1183 -
1184 \begin_inset Quotes erd
1185 \end_inset
1186
1187 .
1188  We can also solve this problem in Sweave directly: set the Sweave option
1189  
1190 \family typewriter
1191 prefix.string
1192 \family default
1193  to be a character string without dots, and this option will override the
1194  default value.
1195 \end_layout
1196
1197 \begin_layout Standard
1198 We have pre-specified an option 
1199 \family typewriter
1200 noae
1201 \family default
1202  for the Sweave package.
1203  This option stops Sweave from loading these two packages:
1204 \end_layout
1205
1206 \begin_layout Standard
1207 \begin_inset ERT
1208 status open
1209
1210 \begin_layout Plain Layout
1211
1212
1213 \backslash
1214 begin{verbatim}
1215 \end_layout
1216
1217 \begin_layout Plain Layout
1218
1219
1220 \backslash
1221 RequirePackage[T1]{fontenc}  
1222 \end_layout
1223
1224 \begin_layout Plain Layout
1225
1226
1227 \backslash
1228 RequirePackage{ae}
1229 \end_layout
1230
1231 \begin_layout Plain Layout
1232
1233
1234 \backslash
1235 end{verbatim}
1236 \end_layout
1237
1238 \end_inset
1239
1240
1241 \end_layout
1242
1243 \begin_layout Standard
1244 LyX can deal with the font encoding by default, so there is no need to ask
1245  Sweave to do this (furthermore, this may bring clashes of package options);
1246  besides, the users can load the 
1247 \series bold
1248 ae
1249 \series default
1250  package by themselves in the preamble if needed.
1251 \end_layout
1252
1253 \begin_layout Section
1254 Trouble Shooting
1255 \end_layout
1256
1257 \begin_layout Standard
1258 Since LyX 2.0, we can see the detailed information during compilation via
1259  
1260 \family sans
1261 View\SpecialChar \menuseparator
1262 View Messages
1263 \family default
1264 .
1265  This is extremely helpful for trouble shooting -- the process of R will
1266  be shown in the message panel, and we will be able to know which chunk
1267  goes wrong in cases of errors.
1268  For example, when you compile this document, you can see messages like
1269  below:
1270 \end_layout
1271
1272 \begin_layout Standard
1273 \begin_inset ERT
1274 status open
1275
1276 \begin_layout Plain Layout
1277
1278
1279 \backslash
1280 begin{verbatim}
1281 \end_layout
1282
1283 \begin_layout Plain Layout
1284
1285 17:58:47.868: Processing code chunks with options ...
1286 \end_layout
1287
1288 \begin_layout Plain Layout
1289
1290 17:58:47.885:  1 : echo keep.source term verbatim (label=setup)
1291 \end_layout
1292
1293 \begin_layout Plain Layout
1294
1295 17:58:47.889:  2 : echo keep.source (label=sweave-manual)
1296 \end_layout
1297
1298 \begin_layout Plain Layout
1299
1300 17:58:47.908:  3 : echo keep.source term verbatim (label=print-integers)
1301 \end_layout
1302
1303 \begin_layout Plain Layout
1304
1305 ....
1306 \end_layout
1307
1308 \begin_layout Plain Layout
1309
1310 17:58:47.941: Loading required package: xtable
1311 \end_layout
1312
1313 \begin_layout Plain Layout
1314
1315 17:58:47.976:  9 : echo term verbatim pdf (label=iris-pairs)
1316 \end_layout
1317
1318 \begin_layout Plain Layout
1319
1320 ....
1321 \end_layout
1322
1323 \begin_layout Plain Layout
1324
1325 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1326 \end_layout
1327
1328 \begin_layout Plain Layout
1329
1330
1331 \backslash
1332 end{verbatim}
1333 \end_layout
1334
1335 \end_inset
1336
1337
1338 \end_layout
1339
1340 \begin_layout Standard
1341 The string after 
1342 \family typewriter
1343 label=
1344 \family default
1345  is the chunk label.
1346  If any chunk causes an error in R, you will see the error message here
1347  immediately.
1348 \end_layout
1349
1350 \begin_layout Section
1351 Resources
1352 \end_layout
1353
1354 \begin_layout Standard
1355 The homepage of Sweave is at 
1356 \begin_inset Flex URL
1357 status collapsed
1358
1359 \begin_layout Plain Layout
1360
1361 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1362 \end_layout
1363
1364 \end_inset
1365
1366 .
1367  The CRAN task view on 
1368 \begin_inset Quotes eld
1369 \end_inset
1370
1371 Reproducible Research
1372 \begin_inset Quotes erd
1373 \end_inset
1374
1375  has a list of Sweave-related packages which can be potentially useful;
1376  see 
1377 \begin_inset Flex URL
1378 status collapsed
1379
1380 \begin_layout Plain Layout
1381
1382 http://cran.r-project.org/web/views/ReproducibleResearch.html
1383 \end_layout
1384
1385 \end_inset
1386
1387 .
1388  Questions about LyX and Sweave can be posted to the LyX mailing list (
1389 \begin_inset Flex URL
1390 status collapsed
1391
1392 \begin_layout Plain Layout
1393
1394 http://www.lyx.org/MailingLists
1395 \end_layout
1396
1397 \end_inset
1398
1399 ).
1400  More LyX examples are provided at 
1401 \begin_inset Flex URL
1402 status collapsed
1403
1404 \begin_layout Plain Layout
1405
1406 https://github.com/yihui/lyx
1407 \end_layout
1408
1409 \end_inset
1410
1411 .
1412 \end_layout
1413
1414 \begin_layout Section
1415 FAQ
1416 \end_layout
1417
1418 \begin_layout Subsection
1419 How to use Sweave with beamer?
1420 \end_layout
1421
1422 \begin_layout Standard
1423 You will end up with LaTeX errors if you use Sweave in beamer slides in
1424  LyX; the reason is the beamer frame that contains verbatim R code and output
1425  must have the 
1426 \family typewriter
1427 fragile
1428 \family default
1429  option.
1430  However, currently there is no straightforward way to do this.
1431  One way is to write raw TeX code, and the other is to use the 
1432 \family sans
1433 beamer-fragile.module
1434 \family default
1435  discussed in 
1436 \begin_inset CommandInset href
1437 LatexCommand href
1438 name "#7273"
1439 target "http://www.lyx.org/trac/ticket/7273"
1440
1441 \end_inset
1442
1443  (put this file under the 
1444 \family sans
1445 layouts
1446 \family default
1447  directory of LyX user directory and reconfigure LyX).
1448  After including this module in your document, you should be able to use
1449  
1450 \family sans
1451 Insert\SpecialChar \menuseparator
1452 Custom Insets\SpecialChar \menuseparator
1453 FragileFrame
1454 \family default
1455 , which is essentially 
1456 \family typewriter
1457
1458 \backslash
1459 begin{frame}[fragile]
1460 \family default
1461 .
1462 \end_layout
1463
1464 \begin_layout Subsection
1465 Can I define my own R script to compile the Rnw document?
1466 \end_layout
1467
1468 \begin_layout Standard
1469 Yes, of course.
1470  First you need to understand how the R script 
1471 \family typewriter
1472 $$s
1473 \family sans
1474 /scripts/lyxsweave.R
1475 \family default
1476  works, which was introduced previously, and we recommend you to read the
1477  R source code as well (on how R takes the paths and uses 
1478 \emph on
1479 Sweave()
1480 \emph default
1481  to handle the Rnw document).
1482  Then you put your customized 
1483 \family sans
1484 lyxsweave.R
1485 \family default
1486  under the 
1487 \family sans
1488 scripts
1489 \family default
1490  directory of your 
1491 \emph on
1492 user directory
1493 \emph default
1494  (again, see 
1495 \family sans
1496 Help\SpecialChar \menuseparator
1497 About LyX
1498 \family default
1499 ).
1500  Note LyX will check the 
1501 \emph on
1502 user directory
1503 \emph default
1504  before going to its own 
1505 \emph on
1506 library directory
1507 \emph default
1508 ; if an R script 
1509 \family sans
1510 lyxsweave.R
1511 \family default
1512  is found in the user directory, it will be used.
1513  It is not recommended to hack the script under the LyX library directory
1514  directly, since updating LyX will override you modified script (the user
1515  directory will not be affected).
1516 \end_layout
1517
1518 \begin_layout Standard
1519 Beside the support for the official Sweave in R, we have a few customized
1520  R scripts to unofficially support other R packages under 
1521 \begin_inset Flex URL
1522 status collapsed
1523
1524 \begin_layout Plain Layout
1525
1526 https://github.com/yihui/lyx
1527 \end_layout
1528
1529 \end_inset
1530
1531 , e.g., the 
1532 \series bold
1533 pgfSweave
1534 \series default
1535  and the 
1536 \series bold
1537 knitr
1538 \series default
1539  package.
1540  The former supports cache and tikz graphics in R, and the latter is an
1541  alternative literate programming engine to Sweave which has even more features.
1542 \end_layout
1543
1544 \end_body
1545 \end_document