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