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