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