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