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