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