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