1 #LyX 2.2 created this file. For more info see http://www.lyx.org/
5 \save_transient_properties true
6 \origin /systemlyxdir/examples/
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)
12 \ifdim\Gin@nat@width>\linewidth
20 \use_default_options true
24 \maintain_unincluded_children false
26 \language_package auto
27 \inputencoding default
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
37 \font_sf_scale 100 100
38 \font_tt_scale 100 100
40 \default_output_format default
42 \bibtex_command default
43 \index_command default
44 \paperfontsize default
49 \use_package amsmath 1
50 \use_package amssymb 1
53 \use_package mathdots 1
54 \use_package mathtools 0
56 \use_package stackrel 0
57 \use_package stmaryrd 0
58 \use_package undertilde 0
60 \cite_engine_type default
64 \paperorientation portrait
78 \paragraph_separation indent
79 \paragraph_indentation default
80 \quotes_language english
83 \paperpagestyle default
84 \tracking_changes false
93 \begin_layout Standard
97 \begin_layout Plain Layout
101 SweaveOpts{prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE}
109 \begin_layout Standard
113 \begin_layout Plain Layout
117 maxwidth was defined in the preamble; see document settings
120 \begin_layout Plain Layout
135 Using Sweave with \SpecialCharNoPassThru LyX
143 \begin_layout Plain Layout
144 Department of Statistics, Iowa State University.
146 \begin_inset CommandInset href
148 name "xie@yihui.name"
149 target "xie@yihui.name"
163 \begin_layout Plain Layout
176 \begin_layout Plain Layout
188 \begin_layout Standard
189 Sweave is a tool in the R language (
190 \begin_inset Flex URL
193 \begin_layout Plain Layout
195 http://www.r-project.org
200 ) for dynamic report generation using the ideas of literate programming.
201 R code can be mixed into a \SpecialCharNoPassThru LaTeX
202 document, and will be executed in R when
203 the document is compiled; the results are written into the \SpecialCharNoPassThru TeX
207 \begin_layout Standard
208 Since \SpecialCharNoPassThru LyX
209 2.0, Sweave can be used out of the box with any document class
219 module to a \SpecialCharNoPassThru LyX
222 Document\SpecialChar menuseparator
223 Settings\SpecialChar menuseparator
227 In this manual, we demonstrate some common features of Sweave and explain
228 how the module works.
229 A PDF version of this manual can be found at
230 \begin_inset Flex URL
233 \begin_layout Plain Layout
235 https://github.com/downloads/yihui/lyx/sweave.pdf
243 \begin_layout Section
247 \begin_layout Standard
252 module depends on the executable
256 , so it has to be in the
260 variable of the system.
261 It is usually not a problem for Linux and Mac OS users, but may be confusing
262 to Windows users; if you do not understand what is
266 under Windows, you may either use the R script
271 \begin_inset Flex URL
274 \begin_layout Plain Layout
276 https://github.com/yihui/lyx
281 to change it automatically (not recommended; you should learn
287 Tools\SpecialChar menuseparator
288 Preferences\SpecialChar menuseparator
289 Paths\SpecialChar menuseparator
292 to add the bin path of R (recommended), which is often like
304 and you can find it in R:
307 \begin_layout Standard
311 \begin_layout Plain Layout
313 <<where-is-R-bin,eval=FALSE>>=
316 \begin_layout Plain Layout
321 \begin_layout Plain Layout
331 \begin_layout Standard
332 After you have done these changes, you need to reconfigure \SpecialCharNoPassThru LyX
335 Tools\SpecialChar menuseparator
351 module will be unavailable.
352 It is recommended to use the latest version of R, since Sweave is still
354 Another dependency is introduced by the \SpecialCharNoPassThru LaTeX
359 which is shipped with R under this path (it is not on CTAN):
362 \begin_layout Standard
366 \begin_layout Plain Layout
368 <<sweave-sty,eval=FALSE>>=
371 \begin_layout Plain Layout
373 file.path(R.home('share'), 'texmf', 'tex', 'latex')
376 \begin_layout Plain Layout
386 \begin_layout Standard
387 Mik\SpecialCharNoPassThru TeX
388 users may add the texmf directory to the list of root directories
389 in Mik\SpecialCharNoPassThru TeX
390 through Mik\SpecialCharNoPassThru TeX
409 If \SpecialCharNoPassThru LyX
414 , it will directly copy from R, so the Sweave module works even if you do
415 not know where is this style file.
418 \begin_layout Section
422 \begin_layout Standard
423 Before we write an Sweave document, we can set some Sweave options; the
430 in Sweave and it can be inserted in a \SpecialCharNoPassThru LyX
433 Insert\SpecialChar menuseparator
434 Custom Insets\SpecialChar menuseparator
437 , or just use \SpecialCharNoPassThru TeX
440 Insert\SpecialChar menuseparator
441 \SpecialCharNoPassThru TeX
445 For example, we can set a global option
449 in the beginning of a document to suppress all R code (by default, both
450 R code and its output will show up in the \SpecialCharNoPassThru LaTeX
455 \begin_layout Standard
456 To write R code in \SpecialCharNoPassThru LyX
457 , you can first switch to the
461 environment in the layout styles list (droplist in the top-left corner
462 in the toolbar), or you can simply open a \SpecialCharNoPassThru TeX
463 code environment and write
464 Sweave code chunks in it.
465 The latter approach is recommended since it is more stable.
468 \begin_layout Standard
469 Some R options can be useful to the Sweave output as well, e.g.
479 The former option can adjust the width of the output, and the latter can
480 avoid a common problem with quotes.
481 Users are encouraged to read the Sweave manual in R for more details.
484 \begin_layout Standard
488 \begin_layout Plain Layout
490 <<setup, keep.source=TRUE>>=
493 \begin_layout Plain Layout
495 ## the option 'digits' can set the (approximate) number of digits
498 \begin_layout Plain Layout
500 options(width = 70, useFancyQuotes = FALSE, digits = 4)
503 \begin_layout Plain Layout
508 \begin_layout Plain Layout
510 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
513 \begin_layout Plain Layout
515 ## view the Sweave vignette (requires R >= 2.13.0)
518 \begin_layout Plain Layout
520 vignette("Sweave", package = "utils")
523 \begin_layout Plain Layout
533 \begin_layout Standard
534 A simple example that will run in any S engine:
537 \begin_layout Standard
541 \begin_layout Plain Layout
543 <<print-integers, keep.source=TRUE>>=
546 \begin_layout Plain Layout
548 ## the integers from 1 to 10 are
551 \begin_layout Plain Layout
556 \begin_layout Plain Layout
561 \begin_layout Plain Layout
563 <<hide-results, results=hide>>=
566 \begin_layout Plain Layout
568 ## intentionally hide the output by setting results=hide
571 \begin_layout Plain Layout
576 \begin_layout Plain Layout
586 \begin_layout Standard
587 We can also emulate a simple calculator:
590 \begin_layout Standard
594 \begin_layout Plain Layout
599 \begin_layout Plain Layout
604 \begin_layout Plain Layout
609 \begin_layout Plain Layout
619 \begin_layout Standard
620 Now we look at some Gaussian data:
623 \begin_layout Standard
627 \begin_layout Plain Layout
632 \begin_layout Plain Layout
634 set.seed(123) # for reproducibility
637 \begin_layout Plain Layout
642 \begin_layout Plain Layout
647 \begin_layout Plain Layout
652 \begin_layout Plain Layout
662 \begin_layout Standard
663 Note that we can easily integrate some numbers into standard text; this
664 is done with the command
670 , and the corresponding \SpecialCharNoPassThru LyX
673 Insert\SpecialChar menuseparator
674 Custom Insets\SpecialChar menuseparator
677 , or alternatively just use \SpecialCharNoPassThru TeX
679 For example, the third element of the vector
684 \begin_inset Flex S/R expression
687 \begin_layout Plain Layout
700 \begin_inset Formula $p$
703 -value of the test is
704 \begin_inset Flex S/R expression
707 \begin_layout Plain Layout
709 format.pval(t1$p.value)
715 You can round these numbers using functions like
723 \begin_layout Standard
733 Now we look at a summary of the
754 \begin_layout Standard
758 \begin_layout Plain Layout
763 \begin_layout Plain Layout
768 \begin_layout Plain Layout
778 \begin_layout Standard
779 \begin_inset Float table
784 \begin_layout Plain Layout
785 \begin_inset Caption Standard
787 \begin_layout Plain Layout
788 Quantiles of the first four variables in the
793 \begin_inset CommandInset label
795 name "tab:xtable-demo"
807 \begin_layout Plain Layout
812 \begin_layout Plain Layout
816 \begin_layout Plain Layout
818 <<xtable-demo,results=tex,echo=FALSE>>=
821 \begin_layout Plain Layout
823 if (require('xtable')) {
826 \begin_layout Plain Layout
828 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
831 \begin_layout Plain Layout
833 } else cat('the xtable package is not available! please install.packages("xtable"
837 \begin_layout Plain Layout
852 \begin_layout Standard
853 Usually people do not like such
854 \begin_inset Quotes eld
858 \begin_inset Quotes erd
861 output, and we can polish these messy numbers with the
867 \begin_inset CommandInset ref
869 reference "tab:xtable-demo"
873 is an example demonstrating how to use the
877 package with Sweave; note the chunk option
881 makes sure that the R output is written out as raw \SpecialCharNoPassThru LaTeX
883 being wrapped in a special environment (
890 \begin_layout Standard
892 \begin_inset CommandInset ref
894 reference "fig:iris-pairs"
899 \begin_inset CommandInset ref
901 reference "fig:iris-boxplots"
905 are two simple examples of producing graphics output with Sweave.
908 \begin_layout Standard
909 \begin_inset Float figure
914 \begin_layout Plain Layout
916 \begin_inset Note Note
919 \begin_layout Plain Layout
920 Both eps and pdf are set to true so that both latex and pdflatex work.
921 In practice, you would probably just choose one.
930 \begin_layout Plain Layout
932 <<iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE>>=
935 \begin_layout Plain Layout
937 pairs(iris, col = iris$Species)
940 \begin_layout Plain Layout
950 \begin_layout Plain Layout
951 \begin_inset Caption Standard
953 \begin_layout Plain Layout
954 Scatter plot matrix of the
959 \begin_inset CommandInset label
961 name "fig:iris-pairs"
978 \begin_layout Standard
979 \begin_inset Float figure
984 \begin_layout Plain Layout
989 \begin_layout Plain Layout
991 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T>>=
994 \begin_layout Plain Layout
996 par(mar=c(4,5,.1,.3), las=1)
999 \begin_layout Plain Layout
1001 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1004 \begin_layout Plain Layout
1014 \begin_layout Plain Layout
1015 \begin_inset Caption Standard
1017 \begin_layout Plain Layout
1018 Boxplots of sepal length grouped by species.
1019 \begin_inset CommandInset label
1021 name "fig:iris-boxplots"
1038 \begin_layout Standard
1039 We can also export all the R code in an Sweave document as a single R script
1042 File\SpecialChar menuseparator
1043 Export\SpecialChar menuseparator
1046 ), which is done via
1053 \begin_layout Section
1061 \begin_layout Standard
1066 module declares that a \SpecialCharNoPassThru LyX
1067 document contains literate programming components
1068 with R, so that \SpecialCharNoPassThru LyX
1069 will handle such documents according to the specific
1070 converters (see the Customization manual).
1079 converter, which essentially consists of an R script
1087 directory of \SpecialCharNoPassThru LyX
1089 This script takes an
1093 document from \SpecialCharNoPassThru LyX
1094 and call Sweave to process it to a \SpecialCharNoPassThru TeX
1096 \SpecialCharNoPassThru LyX
1097 takes care of the rest of work.
1100 \begin_layout Standard
1101 The converter from the
1105 format to \SpecialCharNoPassThru LaTeX
1106 is a call to R via the command line as:
1109 \begin_layout Standard
1113 \begin_layout Plain Layout
1120 \begin_layout Plain Layout
1122 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1126 \begin_layout Plain Layout
1138 \begin_layout Standard
1139 The \SpecialCharNoPassThru LyX
1144 can be found from the menu
1146 Help\SpecialChar menuseparator
1147 About \SpecialCharNoPassThru LyX
1150 All the rest of options passed to R are explained in the R script, and
1151 the \SpecialCharNoPassThru LyX
1152 Customization manual also has detailed explanations about
1169 General users do not need to know clearly what they mean, but here we need
1170 to explain a few issues in the R code:
1173 \begin_layout Enumerate
1174 the encoding string of the \SpecialCharNoPassThru LyX
1175 document is passed to R as an command line
1176 option; possible values are
1185 The encoding is used for R to read the
1192 \begin_layout Enumerate
1193 the R code chunks in the Sweave document are executed under the directory
1194 of the \SpecialCharNoPassThru LyX
1195 document (if you are not sure, put
1199 in a code chunk to see what is the current working directory).
1200 In this case, you can put data files under the same directory and use relative
1201 paths in R to guarantee reproducibility, i.e.
1202 we do not need to write hard-coded paths which may not exist in other systems.
1205 \begin_layout Enumerate
1206 if \SpecialCharNoPassThru LaTeX
1211 , this R script will copy it to the temporary directory automatically where
1212 the \SpecialCharNoPassThru LaTeX
1213 compilation takes place.
1216 \begin_layout Enumerate
1217 Sweave will use the filename of the Rnw file as the prefix string for the
1218 graphics output by default; one known issue (
1219 \begin_inset CommandInset href
1222 target "http://www.lyx.org/trac/ticket/7551"
1226 ) is that if the filename (without extension) contains dots, \SpecialCharNoPassThru LaTeX
1228 to deal with such files in
1235 Therefore, we have set the default value of the prefix to be the filename
1236 with all the dots being replaced by
1237 \begin_inset Quotes eld
1241 \begin_inset Quotes erd
1245 We can also solve this problem in Sweave directly: set the Sweave option
1250 to be a character string without dots, and this option will override the
1254 \begin_layout Standard
1255 We have pre-specified an option
1259 for the Sweave package.
1260 This option stops Sweave from loading these two packages:
1263 \begin_layout Standard
1267 \begin_layout Plain Layout
1274 \begin_layout Plain Layout
1278 RequirePackage[T1]{fontenc}
1281 \begin_layout Plain Layout
1288 \begin_layout Plain Layout
1300 \begin_layout Standard
1301 \SpecialCharNoPassThru LyX
1302 can deal with the font encoding by default, so there is no need to ask
1303 Sweave to do this (furthermore, this may bring clashes of package options);
1304 besides, the users can load the
1308 package by themselves in the preamble if needed.
1311 \begin_layout Section
1315 \begin_layout Standard
1316 Since \SpecialCharNoPassThru LyX
1317 2.0, we can see the detailed information during compilation via
1320 View\SpecialChar menuseparator
1324 This is extremely helpful for trouble shooting \twohyphens
1325 the process of R will
1326 be shown in the message panel, and we will be able to know which chunk
1327 goes wrong in cases of errors.
1328 For example, when you compile this document, you can see messages like
1332 \begin_layout Standard
1336 \begin_layout Plain Layout
1343 \begin_layout Plain Layout
1345 17:58:47.868: Processing code chunks with options ...
1348 \begin_layout Plain Layout
1350 17:58:47.885: 1 : echo keep.source term verbatim (label=setup)
1353 \begin_layout Plain Layout
1355 17:58:47.889: 2 : echo keep.source (label=sweave-manual)
1358 \begin_layout Plain Layout
1360 17:58:47.908: 3 : echo keep.source term verbatim (label=print-integers)
1363 \begin_layout Plain Layout
1368 \begin_layout Plain Layout
1370 17:58:47.941: Loading required package: xtable
1373 \begin_layout Plain Layout
1375 17:58:47.976: 9 : echo term verbatim pdf (label=iris-pairs)
1378 \begin_layout Plain Layout
1383 \begin_layout Plain Layout
1385 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1388 \begin_layout Plain Layout
1400 \begin_layout Standard
1406 If any chunk causes an error in R, you will see the error message here
1410 \begin_layout Standard
1411 Another known issue is the Sweave code chunk may fail when we change the
1412 alignment of the chunk paragraph in \SpecialCharNoPassThru LyX
1417 ), because \SpecialCharNoPassThru LyX
1418 may add a macro like
1424 before the code chunk without adding a new line, which will lead to a code
1428 \begin_layout Standard
1432 \begin_layout Plain Layout
1439 \begin_layout Plain Layout
1443 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1446 \begin_layout Plain Layout
1458 \begin_layout Standard
1459 This is not a legitimate Sweave code chunk, since
1463 must start in a new line with nothing before it.
1464 Therefore we recommend you to double check the real source code via
1466 View\SpecialChar menuseparator
1469 when changing the alignment of a chunk paragraph, and make sure
1473 appears in a new line.
1474 This explains why we manually added an empty line in the code chunk in
1476 \begin_inset CommandInset ref
1478 reference "tab:xtable-demo"
1485 \begin_layout Section
1489 \begin_layout Standard
1490 The homepage of Sweave is at
1491 \begin_inset Flex URL
1494 \begin_layout Plain Layout
1496 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1502 The CRAN task view on
1503 \begin_inset Quotes eld
1506 Reproducible Research
1507 \begin_inset Quotes erd
1510 has a list of Sweave-related packages which can be potentially useful;
1512 \begin_inset Flex URL
1515 \begin_layout Plain Layout
1517 http://cran.r-project.org/web/views/ReproducibleResearch.html
1523 Questions about \SpecialCharNoPassThru LyX
1524 and Sweave can be posted to the \SpecialCharNoPassThru LyX
1526 \begin_inset Flex URL
1529 \begin_layout Plain Layout
1531 http://www.lyx.org/MailingLists
1537 More \SpecialCharNoPassThru LyX
1538 examples are provided at
1539 \begin_inset Flex URL
1542 \begin_layout Plain Layout
1544 https://github.com/yihui/lyx
1552 \begin_layout Section
1556 \begin_layout Subsection
1557 How to use Sweave with beamer?
1560 \begin_layout Standard
1561 You will end up with \SpecialCharNoPassThru LaTeX
1562 errors if you use Sweave in normal beamer slides
1563 in \SpecialCharNoPassThru LyX
1564 ; the reason is the beamer frame that contains verbatim R code.
1565 The solution is to use a
1573 , or to pass the option
1577 to the frame title via
1579 Insert\SpecialChar menuseparator
1585 \begin_layout Subsection
1586 Can I define my own R script to compile the Rnw document?
1589 \begin_layout Standard
1591 First you need to understand how the R script
1595 /scripts/lyxsweave.R
1597 works, which was introduced previously, and we recommend you to read the
1598 R source code as well (on how R takes the paths and uses
1602 to handle the Rnw document).
1603 Then you put your customized
1617 Help\SpecialChar menuseparator
1618 About \SpecialCharNoPassThru LyX
1621 Note \SpecialCharNoPassThru LyX
1626 before going to its own
1634 is found in the user directory, it will be used.
1635 It is not recommended to hack the script under the \SpecialCharNoPassThru LyX
1637 directly, since updating \SpecialCharNoPassThru LyX
1638 will override you modified script (the user
1639 directory will not be affected).
1642 \begin_layout Standard
1643 Beside the support for the official Sweave in R, the
1647 package is also supported, which is an alternative tool to Sweave; the
1648 corresponding module is named