]> git.lyx.org Git - lyx.git/blob - lib/examples/sweave.lyx
Update docs
[lyx.git] / lib / examples / sweave.lyx
1 #LyX 2.2 created this file. For more info see http://www.lyx.org/
2 \lyxformat 504
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 \SpecialCharNoPassThru LyX
136 \end_layout
137
138 \begin_layout Author
139 Yihui Xie
140 \begin_inset Foot
141 status collapsed
142
143 \begin_layout Plain Layout
144 Department of Statistics, Iowa State University.
145  Email: 
146 \begin_inset CommandInset href
147 LatexCommand href
148 name "xie@yihui.name"
149 target "xie@yihui.name"
150 type "mailto:"
151
152 \end_inset
153
154
155 \end_layout
156
157 \end_inset
158
159  
160 \begin_inset ERT
161 status collapsed
162
163 \begin_layout Plain Layout
164
165
166 \backslash
167 and
168 \end_layout
169
170 \end_inset
171
172  Gregor Gorjanc 
173 \begin_inset ERT
174 status collapsed
175
176 \begin_layout Plain Layout
177
178
179 \backslash
180 and
181 \end_layout
182
183 \end_inset
184
185  Jean-Marc Lasgouttes
186 \end_layout
187
188 \begin_layout Standard
189 Sweave is a tool in the R language (
190 \begin_inset Flex URL
191 status collapsed
192
193 \begin_layout Plain Layout
194
195 http://www.r-project.org
196 \end_layout
197
198 \end_inset
199
200 ) for dynamic report generation using the ideas of literate programming.
201  R code can be mixed into a \SpecialCharNoPassThru LaTeX
202  document, and will be executed in R when
203  the document is compiled; the results are written into the \SpecialCharNoPassThru TeX
204  output.
205 \end_layout
206
207 \begin_layout Standard
208 Since \SpecialCharNoPassThru LyX
209  2.0, Sweave can be used out of the box with any document class
210  using the 
211 \family sans
212 Sweave
213 \family default
214  module.
215  We can add the 
216 \family sans
217 Sweave
218 \family default
219  module to a \SpecialCharNoPassThru LyX
220  document from 
221 \family sans
222 Document\SpecialChar menuseparator
223 Settings\SpecialChar menuseparator
224 Modules
225 \family default
226 .
227  In this manual, we demonstrate some common features of Sweave and explain
228  how the module works.
229  A PDF version of this manual can be found at 
230 \begin_inset Flex URL
231 status collapsed
232
233 \begin_layout Plain Layout
234
235 https://github.com/downloads/yihui/lyx/sweave.pdf
236 \end_layout
237
238 \end_inset
239
240 .
241 \end_layout
242
243 \begin_layout Section
244 System Requirements
245 \end_layout
246
247 \begin_layout Standard
248 The 
249 \family sans
250 Sweave
251 \family default
252  module depends on the executable 
253 \family typewriter
254 Rscript
255 \family default
256 , so it has to be in the 
257 \family sans
258 PATH
259 \family default
260  variable of the system.
261  It is usually not a problem for Linux and Mac OS users, but may be confusing
262  to Windows users; if you do not understand what is 
263 \family sans
264 PATH
265 \family default
266  under Windows, you may either use the R script 
267 \family sans
268 add-R-path-win.R
269 \family default
270  in the repository 
271 \begin_inset Flex URL
272 status collapsed
273
274 \begin_layout Plain Layout
275
276 https://github.com/yihui/lyx
277 \end_layout
278
279 \end_inset
280
281  to change it automatically (not recommended; you should learn 
282 \family sans
283 PATH
284 \family default
285  anyway), or go to 
286 \family sans
287 Tools\SpecialChar menuseparator
288 Preferences\SpecialChar menuseparator
289 Paths\SpecialChar menuseparator
290 PATH prefix
291 \family default
292  to add the bin path of R (recommended), which is often like 
293 \family sans
294 C:
295 \backslash
296 Program Files
297 \backslash
298 R
299 \backslash
300 R-x.x.x
301 \backslash
302 bin
303 \family default
304  and you can find it in R:
305 \end_layout
306
307 \begin_layout Standard
308 \begin_inset ERT
309 status open
310
311 \begin_layout Plain Layout
312
313 <<where-is-R-bin,eval=FALSE>>=
314 \end_layout
315
316 \begin_layout Plain Layout
317
318 R.home('bin')
319 \end_layout
320
321 \begin_layout Plain Layout
322
323 @
324 \end_layout
325
326 \end_inset
327
328
329 \end_layout
330
331 \begin_layout Standard
332 After you have done these changes, you need to reconfigure \SpecialCharNoPassThru LyX
333  by 
334 \family sans
335 Tools\SpecialChar menuseparator
336 Reconfigure
337 \family default
338 .
339  If 
340 \family typewriter
341 Rscript
342 \family default
343  is not present in 
344 \family sans
345 PATH
346 \family default
347 , the 
348 \family sans
349 Sweave
350 \family default
351  module will be unavailable.
352  It is recommended to use the latest version of R, since Sweave is still
353  being updated.
354  Another dependency is introduced by the \SpecialCharNoPassThru LaTeX
355  style file 
356 \family sans
357 Sweave.sty
358 \family default
359  which is shipped with R under this path (it is not on CTAN):
360 \end_layout
361
362 \begin_layout Standard
363 \begin_inset ERT
364 status open
365
366 \begin_layout Plain Layout
367
368 <<sweave-sty,eval=FALSE>>=
369 \end_layout
370
371 \begin_layout Plain Layout
372
373 file.path(R.home('share'), 'texmf', 'tex', 'latex')
374 \end_layout
375
376 \begin_layout Plain Layout
377
378 @
379 \end_layout
380
381 \end_inset
382
383
384 \end_layout
385
386 \begin_layout Standard
387 Mik\SpecialCharNoPassThru TeX
388  users may add the texmf directory to the list of root directories
389  in Mik\SpecialCharNoPassThru TeX
390  through Mik\SpecialCharNoPassThru TeX
391  settings.
392  Further note that 
393 \family sans
394 Sweave.sty
395 \family default
396  depends on the 
397 \series bold
398 fancyvrb
399 \series default
400  package and 
401 \emph on
402 may
403 \emph default
404  depend on 
405 \series bold
406 ae
407 \series default
408 .
409  If \SpecialCharNoPassThru LyX
410  cannot find 
411 \family sans
412 Sweave.sty
413 \family default
414 , it will directly copy from R, so the Sweave module works even if you do
415  not know where is this style file.
416 \end_layout
417
418 \begin_layout Section
419 Sweave Examples
420 \end_layout
421
422 \begin_layout Standard
423 Before we write an Sweave document, we can set some Sweave options; the
424  command is 
425 \family typewriter
426
427 \backslash
428 SweaveOpts{}
429 \family default
430  in Sweave and it can be inserted in a \SpecialCharNoPassThru LyX
431  document by 
432 \family sans
433 Insert\SpecialChar menuseparator
434 Custom Insets\SpecialChar menuseparator
435 Sweave Options
436 \family default
437 , or just use \SpecialCharNoPassThru TeX
438  code to write it (
439 \family sans
440 Insert\SpecialChar menuseparator
441 \SpecialCharNoPassThru TeX
442  Code
443 \family default
444 ).
445  For example, we can set a global option 
446 \family typewriter
447 echo=FALSE
448 \family default
449  in the beginning of a document to suppress all R code (by default, both
450  R code and its output will show up in the \SpecialCharNoPassThru LaTeX
451  document).
452  
453 \end_layout
454
455 \begin_layout Standard
456 To write R code in \SpecialCharNoPassThru LyX
457 , you can first switch to the 
458 \family typewriter
459 Chunk
460 \family default
461  environment in the layout styles list (droplist in the top-left corner
462  in the toolbar), or you can simply open a \SpecialCharNoPassThru TeX
463  code environment and write
464  Sweave code chunks in it.
465  The latter approach is recommended since it is more stable.
466 \end_layout
467
468 \begin_layout Standard
469 Some R options can be useful to the Sweave output as well, e.g.
470  
471 \family typewriter
472 width
473 \family default
474  and 
475 \family typewriter
476 useFancyQuotes
477 \family default
478 .
479  The former option can adjust the width of the output, and the latter can
480  avoid a common problem with quotes.
481  Users are encouraged to read the Sweave manual in R for more details.
482 \end_layout
483
484 \begin_layout Standard
485 \begin_inset ERT
486 status open
487
488 \begin_layout Plain Layout
489
490 <<setup, keep.source=TRUE>>=
491 \end_layout
492
493 \begin_layout Plain Layout
494
495 ## the option 'digits' can set the (approximate) number of digits
496 \end_layout
497
498 \begin_layout Plain Layout
499
500 options(width = 70, useFancyQuotes = FALSE, digits = 4)
501 \end_layout
502
503 \begin_layout Plain Layout
504
505 @
506 \end_layout
507
508 \begin_layout Plain Layout
509
510 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
511 \end_layout
512
513 \begin_layout Plain Layout
514
515 ## view the Sweave vignette (requires R >= 2.13.0)
516 \end_layout
517
518 \begin_layout Plain Layout
519
520 vignette("Sweave", package = "utils")
521 \end_layout
522
523 \begin_layout Plain Layout
524
525 @
526 \end_layout
527
528 \end_inset
529
530
531 \end_layout
532
533 \begin_layout Standard
534 A simple example that will run in any S engine: 
535 \end_layout
536
537 \begin_layout Standard
538 \begin_inset ERT
539 status open
540
541 \begin_layout Plain Layout
542
543 <<print-integers, keep.source=TRUE>>=
544 \end_layout
545
546 \begin_layout Plain Layout
547
548 ## the integers from 1 to 10 are
549 \end_layout
550
551 \begin_layout Plain Layout
552
553 1:10 
554 \end_layout
555
556 \begin_layout Plain Layout
557
558 @
559 \end_layout
560
561 \begin_layout Plain Layout
562
563 <<hide-results, results=hide>>=
564 \end_layout
565
566 \begin_layout Plain Layout
567
568 ## intentionally hide the output by setting results=hide
569 \end_layout
570
571 \begin_layout Plain Layout
572
573 print(1:20)
574 \end_layout
575
576 \begin_layout Plain Layout
577
578 @
579 \end_layout
580
581 \end_inset
582
583
584 \end_layout
585
586 \begin_layout Standard
587 We can also emulate a simple calculator: 
588 \end_layout
589
590 \begin_layout Standard
591 \begin_inset ERT
592 status open
593
594 \begin_layout Plain Layout
595
596 <<calculator>>=
597 \end_layout
598
599 \begin_layout Plain Layout
600
601 1 + pi
602 \end_layout
603
604 \begin_layout Plain Layout
605
606 sin(pi/6)
607 \end_layout
608
609 \begin_layout Plain Layout
610
611 @
612 \end_layout
613
614 \end_inset
615
616
617 \end_layout
618
619 \begin_layout Standard
620 Now we look at some Gaussian data:
621 \end_layout
622
623 \begin_layout Standard
624 \begin_inset ERT
625 status open
626
627 \begin_layout Plain Layout
628
629 <<rnorm>>=
630 \end_layout
631
632 \begin_layout Plain Layout
633
634 set.seed(123)  # for reproducibility
635 \end_layout
636
637 \begin_layout Plain Layout
638
639 x <- rnorm(20)
640 \end_layout
641
642 \begin_layout Plain Layout
643
644 print(x)
645 \end_layout
646
647 \begin_layout Plain Layout
648
649 t1 <- t.test(x)
650 \end_layout
651
652 \begin_layout Plain Layout
653
654 @
655 \end_layout
656
657 \end_inset
658
659
660 \end_layout
661
662 \begin_layout Standard
663 Note that we can easily integrate some numbers into standard text; this
664  is done with the command 
665 \family typewriter
666
667 \backslash
668 Sexpr{}
669 \family default
670 , and the corresponding \SpecialCharNoPassThru LyX
671  menu is 
672 \family sans
673 Insert\SpecialChar menuseparator
674 Custom Insets\SpecialChar menuseparator
675 S/R expression
676 \family default
677 , or alternatively just use \SpecialCharNoPassThru TeX
678  code.
679  For example, the third element of the vector 
680 \family typewriter
681 x
682 \family default
683  is 
684 \begin_inset Flex S/R expression
685 status collapsed
686
687 \begin_layout Plain Layout
688
689 x[3]
690 \end_layout
691
692 \end_inset
693
694  (i.e.
695  
696 \family typewriter
697 x[3]
698 \family default
699 ) and the 
700 \begin_inset Formula $p$
701 \end_inset
702
703 -value of the test is 
704 \begin_inset Flex S/R expression
705 status collapsed
706
707 \begin_layout Plain Layout
708
709 format.pval(t1$p.value)
710 \end_layout
711
712 \end_inset
713
714 .
715  You can round these numbers using functions like 
716 \emph on
717 round()
718 \emph default
719  in R.
720  
721 \end_layout
722
723 \begin_layout Standard
724
725 \family roman
726 \series medium
727 \shape up
728 \size normal
729 \emph off
730 \bar no
731 \noun off
732 \color none
733 Now we look at a summary of the 
734 \family typewriter
735 \series default
736 \shape default
737 \size default
738 \emph default
739 \bar default
740 \noun default
741 \color inherit
742 iris
743 \family roman
744 \series medium
745 \shape up
746 \size normal
747 \emph off
748 \bar no
749 \noun off
750 \color none
751  dataset:
752 \end_layout
753
754 \begin_layout Standard
755 \begin_inset ERT
756 status open
757
758 \begin_layout Plain Layout
759
760 <<iris-summary>>=
761 \end_layout
762
763 \begin_layout Plain Layout
764
765 summary(iris[,-5])
766 \end_layout
767
768 \begin_layout Plain Layout
769
770 @
771 \end_layout
772
773 \end_inset
774
775
776 \end_layout
777
778 \begin_layout Standard
779 \begin_inset Float table
780 wide false
781 sideways false
782 status open
783
784 \begin_layout Plain Layout
785 \begin_inset Caption Standard
786
787 \begin_layout Plain Layout
788 Quantiles of the first four variables in the 
789 \family typewriter
790 iris
791 \family default
792  data.
793 \begin_inset CommandInset label
794 LatexCommand label
795 name "tab:xtable-demo"
796
797 \end_inset
798
799
800 \end_layout
801
802 \end_inset
803
804
805 \end_layout
806
807 \begin_layout Plain Layout
808 \align center
809 \begin_inset ERT
810 status open
811
812 \begin_layout Plain Layout
813
814 \end_layout
815
816 \begin_layout Plain Layout
817
818 <<xtable-demo,results=tex,echo=FALSE>>=
819 \end_layout
820
821 \begin_layout Plain Layout
822
823 if (require('xtable')) {
824 \end_layout
825
826 \begin_layout Plain Layout
827
828 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
829 \end_layout
830
831 \begin_layout Plain Layout
832
833 } else cat('the xtable package is not available! please install.packages("xtable"
834 )')
835 \end_layout
836
837 \begin_layout Plain Layout
838
839 @
840 \end_layout
841
842 \end_inset
843
844
845 \end_layout
846
847 \end_inset
848
849
850 \end_layout
851
852 \begin_layout Standard
853 Usually people do not like such 
854 \begin_inset Quotes eld
855 \end_inset
856
857 raw
858 \begin_inset Quotes erd
859 \end_inset
860
861  output, and we can polish these messy numbers with the 
862 \series bold
863 xtable
864 \series default
865  package.
866  Table 
867 \begin_inset CommandInset ref
868 LatexCommand ref
869 reference "tab:xtable-demo"
870
871 \end_inset
872
873  is an example demonstrating how to use the 
874 \series bold
875 xtable
876 \series default
877  package with Sweave; note the chunk option 
878 \family typewriter
879 results=tex
880 \family default
881  makes sure that the R output is written out as raw \SpecialCharNoPassThru LaTeX
882  code instead of
883  being wrapped in a special environment (
884 \family typewriter
885 Soutput
886 \family default
887 ).
888 \end_layout
889
890 \begin_layout Standard
891 Figure 
892 \begin_inset CommandInset ref
893 LatexCommand ref
894 reference "fig:iris-pairs"
895
896 \end_inset
897
898  and 
899 \begin_inset CommandInset ref
900 LatexCommand ref
901 reference "fig:iris-boxplots"
902
903 \end_inset
904
905  are two simple examples of producing graphics output with Sweave.
906 \end_layout
907
908 \begin_layout Standard
909 \begin_inset Float figure
910 wide false
911 sideways false
912 status open
913
914 \begin_layout Plain Layout
915 \align center
916 \begin_inset Note Note
917 status open
918
919 \begin_layout Plain Layout
920 Both eps and pdf are set to true so that both latex and pdflatex work.
921  In practice, you would probably just choose one.
922 \end_layout
923
924 \end_inset
925
926
927 \begin_inset ERT
928 status open
929
930 \begin_layout Plain Layout
931
932 <<iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE>>=
933 \end_layout
934
935 \begin_layout Plain Layout
936
937 pairs(iris, col = iris$Species)
938 \end_layout
939
940 \begin_layout Plain Layout
941
942 @
943 \end_layout
944
945 \end_inset
946
947
948 \end_layout
949
950 \begin_layout Plain Layout
951 \begin_inset Caption Standard
952
953 \begin_layout Plain Layout
954 Scatter plot matrix of the 
955 \family typewriter
956 iris
957 \family default
958  data.
959 \begin_inset CommandInset label
960 LatexCommand label
961 name "fig:iris-pairs"
962
963 \end_inset
964
965
966 \end_layout
967
968 \end_inset
969
970
971 \end_layout
972
973 \end_inset
974
975
976 \end_layout
977
978 \begin_layout Standard
979 \begin_inset Float figure
980 wide false
981 sideways false
982 status open
983
984 \begin_layout Plain Layout
985 \align center
986 \begin_inset ERT
987 status open
988
989 \begin_layout Plain Layout
990
991 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T>>=
992 \end_layout
993
994 \begin_layout Plain Layout
995
996 par(mar=c(4,5,.1,.3), las=1)
997 \end_layout
998
999 \begin_layout Plain Layout
1000
1001 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1002 \end_layout
1003
1004 \begin_layout Plain Layout
1005
1006 @
1007 \end_layout
1008
1009 \end_inset
1010
1011
1012 \end_layout
1013
1014 \begin_layout Plain Layout
1015 \begin_inset Caption Standard
1016
1017 \begin_layout Plain Layout
1018 Boxplots of sepal length grouped by species.
1019 \begin_inset CommandInset label
1020 LatexCommand label
1021 name "fig:iris-boxplots"
1022
1023 \end_inset
1024
1025
1026 \end_layout
1027
1028 \end_inset
1029
1030
1031 \end_layout
1032
1033 \end_inset
1034
1035
1036 \end_layout
1037
1038 \begin_layout Standard
1039 We can also export all the R code in an Sweave document as a single R script
1040  (
1041 \family sans
1042 File\SpecialChar menuseparator
1043 Export\SpecialChar menuseparator
1044 R/S code
1045 \family default
1046 ), which is done via 
1047 \family typewriter
1048 R CMD Stangle
1049 \family default
1050 .
1051 \end_layout
1052
1053 \begin_layout Section
1054 The 
1055 \family sans
1056 Sweave
1057 \family default
1058  Module
1059 \end_layout
1060
1061 \begin_layout Standard
1062 The 
1063 \family sans
1064 Sweave
1065 \family default
1066  module declares that a \SpecialCharNoPassThru LyX
1067  document contains literate programming components
1068  with R, so that \SpecialCharNoPassThru LyX
1069  will handle such documents according to the specific
1070  converters (see the Customization manual).
1071  The 
1072 \family sans
1073 Sweave
1074 \family default
1075  module uses the 
1076 \family sans
1077 sweave
1078 \family default
1079  converter, which essentially consists of an R script 
1080 \family sans
1081 lyxsweave.R
1082 \family default
1083  under the 
1084 \family sans
1085 scripts
1086 \family default
1087  directory of \SpecialCharNoPassThru LyX
1088 .
1089  This script takes an 
1090 \family sans
1091 Rnw
1092 \family default
1093  document from \SpecialCharNoPassThru LyX
1094  and call Sweave to process it to a \SpecialCharNoPassThru TeX
1095  document, then
1096  \SpecialCharNoPassThru LyX
1097  takes care of the rest of work.
1098 \end_layout
1099
1100 \begin_layout Standard
1101 The converter from the 
1102 \family sans
1103 sweave
1104 \family default
1105  format to \SpecialCharNoPassThru LaTeX
1106  is a call to R via the command line as:
1107 \end_layout
1108
1109 \begin_layout Standard
1110 \begin_inset ERT
1111 status open
1112
1113 \begin_layout Plain Layout
1114
1115
1116 \backslash
1117 begin{verbatim}
1118 \end_layout
1119
1120 \begin_layout Plain Layout
1121
1122 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1123  $$r
1124 \end_layout
1125
1126 \begin_layout Plain Layout
1127
1128
1129 \backslash
1130 end{verbatim}
1131 \end_layout
1132
1133 \end_inset
1134
1135
1136 \end_layout
1137
1138 \begin_layout Standard
1139 The \SpecialCharNoPassThru LyX
1140  library directory 
1141 \family typewriter
1142 $$s
1143 \family default
1144  can be found from the menu 
1145 \family sans
1146 Help\SpecialChar menuseparator
1147 About \SpecialCharNoPassThru LyX
1148 \family default
1149 .
1150  All the rest of options passed to R are explained in the R script, and
1151  the \SpecialCharNoPassThru LyX
1152  Customization manual also has detailed explanations about 
1153 \family typewriter
1154 $$i
1155 \family default
1156
1157 \family typewriter
1158 $$o
1159 \family default
1160
1161 \family typewriter
1162 $$e
1163 \family default
1164  and 
1165 \family typewriter
1166 $$r
1167 \family default
1168 .
1169  General users do not need to know clearly what they mean, but here we need
1170  to explain a few issues in the R code:
1171 \end_layout
1172
1173 \begin_layout Enumerate
1174 the encoding string of the \SpecialCharNoPassThru LyX
1175  document is passed to R as an command line
1176  option; possible values are 
1177 \family typewriter
1178 ISO-8859-15
1179 \family default
1180  and 
1181 \family typewriter
1182 UTF-8
1183 \family default
1184 , etc.
1185  The encoding is used for R to read the 
1186 \family sans
1187 Rnw
1188 \family default
1189  document correctly.
1190 \end_layout
1191
1192 \begin_layout Enumerate
1193 the R code chunks in the Sweave document are executed under the directory
1194  of the \SpecialCharNoPassThru LyX
1195  document (if you are not sure, put 
1196 \family typewriter
1197 getwd()
1198 \family default
1199  in a code chunk to see what is the current working directory).
1200  In this case, you can put data files under the same directory and use relative
1201  paths in R to guarantee reproducibility, i.e.
1202  we do not need to write hard-coded paths which may not exist in other systems.
1203 \end_layout
1204
1205 \begin_layout Enumerate
1206 if \SpecialCharNoPassThru LaTeX
1207  cannot find 
1208 \family sans
1209 Sweave.sty
1210 \family default
1211 , this R script will copy it to the temporary directory automatically where
1212  the \SpecialCharNoPassThru LaTeX
1213  compilation takes place.
1214 \end_layout
1215
1216 \begin_layout Enumerate
1217 Sweave will use the filename of the Rnw file as the prefix string for the
1218  graphics output by default; one known issue (
1219 \begin_inset CommandInset href
1220 LatexCommand href
1221 name "#7551"
1222 target "http://www.lyx.org/trac/ticket/7551"
1223
1224 \end_inset
1225
1226 ) is that if the filename (without extension) contains dots, \SpecialCharNoPassThru LaTeX
1227  can fail
1228  to deal with such files in 
1229 \family typewriter
1230
1231 \backslash
1232 includegraphics{}
1233 \family default
1234 .
1235  Therefore, we have set the default value of the prefix to be the filename
1236  with all the dots being replaced by 
1237 \begin_inset Quotes eld
1238 \end_inset
1239
1240 -
1241 \begin_inset Quotes erd
1242 \end_inset
1243
1244 .
1245  We can also solve this problem in Sweave directly: set the Sweave option
1246  
1247 \family typewriter
1248 prefix.string
1249 \family default
1250  to be a character string without dots, and this option will override the
1251  default value.
1252 \end_layout
1253
1254 \begin_layout Standard
1255 We have pre-specified an option 
1256 \family typewriter
1257 noae
1258 \family default
1259  for the Sweave package.
1260  This option stops Sweave from loading these two packages:
1261 \end_layout
1262
1263 \begin_layout Standard
1264 \begin_inset ERT
1265 status open
1266
1267 \begin_layout Plain Layout
1268
1269
1270 \backslash
1271 begin{verbatim}
1272 \end_layout
1273
1274 \begin_layout Plain Layout
1275
1276
1277 \backslash
1278 RequirePackage[T1]{fontenc}  
1279 \end_layout
1280
1281 \begin_layout Plain Layout
1282
1283
1284 \backslash
1285 RequirePackage{ae}
1286 \end_layout
1287
1288 \begin_layout Plain Layout
1289
1290
1291 \backslash
1292 end{verbatim}
1293 \end_layout
1294
1295 \end_inset
1296
1297
1298 \end_layout
1299
1300 \begin_layout Standard
1301 \SpecialCharNoPassThru LyX
1302  can deal with the font encoding by default, so there is no need to ask
1303  Sweave to do this (furthermore, this may bring clashes of package options);
1304  besides, the users can load the 
1305 \series bold
1306 ae
1307 \series default
1308  package by themselves in the preamble if needed.
1309 \end_layout
1310
1311 \begin_layout Section
1312 Trouble Shooting
1313 \end_layout
1314
1315 \begin_layout Standard
1316 Since \SpecialCharNoPassThru LyX
1317  2.0, we can see the detailed information during compilation via
1318  
1319 \family sans
1320 View\SpecialChar menuseparator
1321 View Messages
1322 \family default
1323 .
1324  This is extremely helpful for trouble shooting \twohyphens
1325  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 \SpecialCharNoPassThru LyX
1413  (using 
1414 \family sans
1415 Paragraph settings
1416 \family default
1417 ), because \SpecialCharNoPassThru 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 \SpecialCharNoPassThru LyX
1524  and Sweave can be posted to the \SpecialCharNoPassThru 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 \SpecialCharNoPassThru 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 \SpecialCharNoPassThru LaTeX
1562  errors if you use Sweave in normal beamer slides
1563  in \SpecialCharNoPassThru 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 \SpecialCharNoPassThru LyX
1619 \family default
1620 ).
1621  Note \SpecialCharNoPassThru LyX
1622  will check the 
1623 \emph on
1624 user directory
1625 \emph default
1626  before going to its own 
1627 \emph on
1628 library directory
1629 \emph default
1630 ; if an R script 
1631 \family sans
1632 lyxsweave.R
1633 \family default
1634  is found in the user directory, it will be used.
1635  It is not recommended to hack the script under the \SpecialCharNoPassThru LyX
1636  library directory
1637  directly, since updating \SpecialCharNoPassThru LyX
1638  will override you modified script (the user
1639  directory will not be affected).
1640 \end_layout
1641
1642 \begin_layout Standard
1643 Beside the support for the official Sweave in R, the 
1644 \series bold
1645 knitr
1646 \series default
1647  package is also supported, which is an alternative tool to Sweave; the
1648  corresponding module is named 
1649 \family sans
1650 Rnw (knitr)
1651 \family default
1652 .
1653 \end_layout
1654
1655 \end_body
1656 \end_document