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