1 #LyX 2.3 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 \use_dash_ligatures false
42 \default_output_format default
44 \bibtex_command default
45 \index_command default
46 \paperfontsize default
51 \use_package amsmath 1
52 \use_package amssymb 1
55 \use_package mathdots 1
56 \use_package mathtools 0
58 \use_package stackrel 0
59 \use_package stmaryrd 0
60 \use_package undertilde 0
62 \cite_engine_type default
66 \paperorientation portrait
81 \paragraph_separation indent
82 \paragraph_indentation default
84 \math_numbering_side default
89 \paperpagestyle default
90 \tracking_changes true
95 \author 274215730 "scott"
100 \begin_layout Standard
101 \begin_inset Note Note
104 \begin_layout Plain Layout
105 \begin_inset Flex Sweave Options
108 \begin_layout Plain Layout
110 prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE
123 \begin_layout Standard
127 \begin_layout Plain Layout
131 maxwidth was defined in the preamble; see document settings
134 \begin_layout Plain Layout
149 Using Sweave with \SpecialChar LyX
158 \begin_layout Plain Layout
159 Department of Statistics, Iowa State University.
161 \begin_inset CommandInset href
163 name "xie@yihui.name"
164 target "xie@yihui.name"
179 \begin_layout Plain Layout
192 \begin_layout Plain Layout
204 \begin_layout Standard
205 Sweave is a tool in the R language (
206 \begin_inset Flex URL
209 \begin_layout Plain Layout
211 http://www.r-project.org
216 ) for dynamic report generation using the ideas of literate programming.
217 R code can be mixed into a \SpecialChar LaTeX
218 document, and will be executed in R when the
219 document is compiled; the results are written into the \SpecialChar TeX
223 \begin_layout Standard
224 Since \SpecialChar LyX
225 2.0, Sweave can be used out of the box with any document class using
235 module to a \SpecialChar LyX
238 Document\SpecialChar menuseparator
239 Settings\SpecialChar menuseparator
243 In this manual, we demonstrate some common features of Sweave and explain
244 how the module works.
245 A PDF version of this manual can be found at
246 \begin_inset Flex URL
249 \begin_layout Plain Layout
251 https://github.com/downloads/yihui/lyx/sweave.pdf
259 \begin_layout Standard
260 Since \SpecialChar LyX
261 2.3, it is necessary to enable the use of
267 Preferences\SpecialChar menuseparator
268 File Handling\SpecialChar menuseparator
271 in order to compile with Sweave.
274 \begin_layout Section
278 \begin_layout Standard
283 module depends on the executable
287 , so it has to be in the
291 variable of the system.
292 It is usually not a problem for Linux and Mac OS users, but may be confusing
293 to Windows users; if you do not understand what is
297 under Windows, you may either use the R script
302 \begin_inset Flex URL
305 \begin_layout Plain Layout
307 https://github.com/yihui/lyx
312 to change it automatically (not recommended; you should learn
318 Tools\SpecialChar menuseparator
319 Preferences\SpecialChar menuseparator
320 Paths\SpecialChar menuseparator
323 to add the bin path of R (recommended), which is often like
335 and you can find it in R:
338 \begin_layout Standard
339 \begin_inset Flex Chunk
342 \begin_layout Plain Layout
344 \begin_inset Argument 1
347 \begin_layout Plain Layout
349 where-is-R-bin,eval=FALSE
357 \begin_layout Plain Layout
367 \begin_layout Standard
368 After you have done these changes, you need to reconfigure \SpecialChar LyX
371 Tools\SpecialChar menuseparator
387 module will be unavailable.
388 It is recommended to use the latest version of R, since Sweave is still
390 Another dependency is introduced by the \SpecialChar LaTeX
395 which is shipped with R under this path (it is not on CTAN):
398 \begin_layout Standard
399 \begin_inset Flex Chunk
402 \begin_layout Plain Layout
404 \begin_inset Argument 1
407 \begin_layout Plain Layout
409 sweave-sty,eval=FALSE
417 \begin_layout Plain Layout
419 file.path(R.home('share'), 'texmf', 'tex', 'latex')
427 \begin_layout Standard
429 users may add the texmf directory to the list of root directories in
431 through Mik\SpecialChar TeX
455 , it will directly copy from R, so the Sweave module works even if you do
456 not know where is this style file.
459 \begin_layout Section
463 \begin_layout Standard
464 Before we write an Sweave document, we can set some Sweave options; the
471 in Sweave and it can be inserted in a \SpecialChar LyX
474 Insert\SpecialChar menuseparator
475 Custom Insets\SpecialChar menuseparator
478 , or just use \SpecialChar TeX
481 Insert\SpecialChar menuseparator
486 For example, we can set a global option
490 in the beginning of a document to suppress all R code (by default, both
491 R code and its output will show up in the \SpecialChar LaTeX
496 \begin_layout Standard
497 To write R code in \SpecialChar LyX
498 , you can first insert
504 Insert\SpecialChar menuseparator
505 Custom Insets\SpecialChar menuseparator
508 , or you can simply open a \SpecialChar TeX
509 code environment and write Sweave code chunks
511 However, \SpecialChar LyX
512 provides a summary of all the
516 insets in the document in the
518 Navigate\SpecialChar menuseparator
523 View\SpecialChar menuseparator
529 \begin_layout Standard
530 Some R options can be useful to the Sweave output as well, e.g.
540 The former option can adjust the width of the output, and the latter can
541 avoid a common problem with quotes.
542 Users are encouraged to read the Sweave manual in R for more details.
545 \begin_layout Standard
546 \begin_inset Flex Chunk
549 \begin_layout Plain Layout
551 \begin_inset Argument 1
554 \begin_layout Plain Layout
556 setup, keep.source=TRUE
564 \begin_layout Plain Layout
566 ## the option 'digits' can set the (approximate) number of digits
569 \begin_layout Plain Layout
571 options(width = 70, useFancyQuotes = FALSE, digits = 4)
579 \begin_layout Standard
580 \begin_inset Flex Chunk
583 \begin_layout Plain Layout
585 \begin_inset Argument 1
588 \begin_layout Plain Layout
590 sweave-manual, eval=FALSE, keep.source=TRUE
598 \begin_layout Plain Layout
600 ## view the Sweave vignette (requires R >= 2.13.0)
603 \begin_layout Plain Layout
605 vignette("Sweave", package = "utils")
613 \begin_layout Standard
614 A simple example that will run in any S engine:
617 \begin_layout Standard
618 \begin_inset Flex Chunk
621 \begin_layout Plain Layout
623 \begin_inset Argument 1
626 \begin_layout Plain Layout
628 print-integers, keep.source=TRUE
636 \begin_layout Plain Layout
638 ## the integers from 1 to 10 are
641 \begin_layout Plain Layout
651 \begin_layout Standard
652 \begin_inset Flex Chunk
655 \begin_layout Plain Layout
657 \begin_inset Argument 1
660 \begin_layout Plain Layout
662 hide-results, results=hide
670 \begin_layout Plain Layout
672 ## intentionally hide the output by setting results=hide
675 \begin_layout Plain Layout
685 \begin_layout Standard
686 We can also emulate a simple calculator:
689 \begin_layout Standard
690 \begin_inset Flex Chunk
693 \begin_layout Plain Layout
695 \begin_inset Argument 1
698 \begin_layout Plain Layout
708 \begin_layout Plain Layout
713 \begin_layout Plain Layout
723 \begin_layout Standard
724 Now we look at some Gaussian data:
727 \begin_layout Standard
728 \begin_inset Flex Chunk
731 \begin_layout Plain Layout
733 \begin_inset Argument 1
736 \begin_layout Plain Layout
746 \begin_layout Plain Layout
748 set.seed(123) # for reproducibility
751 \begin_layout Plain Layout
756 \begin_layout Plain Layout
761 \begin_layout Plain Layout
771 \begin_layout Standard
772 Note that we can easily integrate some numbers into standard text; this
773 is done with the command
779 , and the corresponding \SpecialChar LyX
782 Insert\SpecialChar menuseparator
783 Custom Insets\SpecialChar menuseparator
786 , or alternatively just use \SpecialChar TeX
788 For example, the third element of the vector
793 \begin_inset Flex S/R expression
796 \begin_layout Plain Layout
809 \begin_inset Formula $p$
812 -value of the test is
813 \begin_inset Flex S/R expression
816 \begin_layout Plain Layout
818 format.pval(t1$p.value)
824 You can round these numbers using functions like
832 \begin_layout Standard
842 Now we look at a summary of the
863 \begin_layout Standard
864 \begin_inset Flex Chunk
867 \begin_layout Plain Layout
869 \begin_inset Argument 1
872 \begin_layout Plain Layout
882 \begin_layout Plain Layout
892 \begin_layout Standard
893 \begin_inset Float table
898 \begin_layout Plain Layout
899 \begin_inset Caption Standard
901 \begin_layout Plain Layout
902 Quantiles of the first four variables in the
907 \begin_inset CommandInset label
909 name "tab:xtable-demo"
919 \begin_inset Flex Chunk
922 \begin_layout Plain Layout
924 \begin_inset Argument 1
927 \begin_layout Plain Layout
929 xtable-demo,results=tex,echo=FALSE
937 \begin_layout Plain Layout
939 if (require('xtable')) {
942 \begin_layout Plain Layout
944 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
947 \begin_layout Plain Layout
949 } else cat('the xtable package is not available! please install.packages("xtable"
963 \begin_layout Standard
964 Usually people do not like such
965 \begin_inset Quotes eld
969 \begin_inset Quotes erd
972 output, and we can polish these messy numbers with the
978 \begin_inset CommandInset ref
980 reference "tab:xtable-demo"
984 is an example demonstrating how to use the
988 package with Sweave; note the chunk option
992 makes sure that the R output is written out as raw \SpecialChar LaTeX
993 code instead of being
994 wrapped in a special environment (
1001 \begin_layout Standard
1003 \begin_inset CommandInset ref
1005 reference "fig:iris-pairs"
1010 \begin_inset CommandInset ref
1012 reference "fig:iris-boxplots"
1016 are two simple examples of producing graphics output with Sweave.
1019 \begin_layout Standard
1020 \begin_inset Float figure
1025 \begin_layout Plain Layout
1027 \begin_inset Note Note
1030 \begin_layout Plain Layout
1031 Both eps and pdf are set to true so that both latex and pdflatex work.
1032 In practice, you would probably just choose one.
1040 \begin_layout Plain Layout
1042 \begin_inset Flex Chunk
1045 \begin_layout Plain Layout
1047 \begin_inset Argument 1
1050 \begin_layout Plain Layout
1052 iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE
1060 \begin_layout Plain Layout
1062 pairs(iris, col = iris$Species)
1070 \begin_layout Plain Layout
1071 \begin_inset Caption Standard
1073 \begin_layout Plain Layout
1074 Scatter plot matrix of the
1079 \begin_inset CommandInset label
1081 name "fig:iris-pairs"
1098 \begin_layout Standard
1099 \begin_inset Float figure
1104 \begin_layout Plain Layout
1106 \begin_inset Flex Chunk
1109 \begin_layout Plain Layout
1111 \begin_inset Argument 1
1114 \begin_layout Plain Layout
1116 iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T
1124 \begin_layout Plain Layout
1126 par(mar=c(4,5,.1,.3), las=1)
1129 \begin_layout Plain Layout
1131 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1139 \begin_layout Plain Layout
1140 \begin_inset Caption Standard
1142 \begin_layout Plain Layout
1143 Boxplots of sepal length grouped by species.
1144 \begin_inset CommandInset label
1146 name "fig:iris-boxplots"
1163 \begin_layout Standard
1164 We can also export all the R code in an Sweave document as a single R script
1167 File\SpecialChar menuseparator
1168 Export\SpecialChar menuseparator
1171 ), which is done via
1178 \begin_layout Section
1186 \begin_layout Standard
1191 module declares that a \SpecialChar LyX
1192 document contains literate programming components
1193 with R, so that \SpecialChar LyX
1194 will handle such documents according to the specific converter
1195 s (see the Customization manual).
1204 converter, which essentially consists of an R script
1212 directory of \SpecialChar LyX
1214 This script takes an
1218 document from \SpecialChar LyX
1219 and call Sweave to process it to a \SpecialChar TeX
1220 document, then \SpecialChar LyX
1222 care of the rest of work.
1225 \begin_layout Standard
1226 The converter from the
1230 format to \SpecialChar LaTeX
1231 is a call to R via the command line as:
1234 \begin_layout Standard
1238 \begin_layout Plain Layout
1245 \begin_layout Plain Layout
1247 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1251 \begin_layout Plain Layout
1263 \begin_layout Standard
1264 The \SpecialChar LyX
1269 can be found from the menu
1271 Help\SpecialChar menuseparator
1272 About \SpecialChar LyX
1276 All the rest of options passed to R are explained in the R script, and
1277 the \SpecialChar LyX
1278 Customization manual also has detailed explanations about
1295 General users do not need to know clearly what they mean, but here we need
1296 to explain a few issues in the R code:
1299 \begin_layout Enumerate
1300 the encoding string of the \SpecialChar LyX
1301 document is passed to R as an command line option;
1311 The encoding is used for R to read the
1318 \begin_layout Enumerate
1319 the R code chunks in the Sweave document are executed under the directory
1320 of the \SpecialChar LyX
1321 document (if you are not sure, put
1325 in a code chunk to see what is the current working directory).
1326 In this case, you can put data files under the same directory and use relative
1327 paths in R to guarantee reproducibility, i.e.
1328 we do not need to write hard-coded paths which may not exist in other systems.
1331 \begin_layout Enumerate
1332 if \SpecialChar LaTeX
1337 , this R script will copy it to the temporary directory automatically where
1338 the \SpecialChar LaTeX
1339 compilation takes place.
1342 \begin_layout Enumerate
1343 Sweave will use the filename of the Rnw file as the prefix string for the
1344 graphics output by default; one known issue (
1345 \begin_inset CommandInset href
1348 target "http://www.lyx.org/trac/ticket/7551"
1353 ) is that if the filename (without extension) contains dots, \SpecialChar LaTeX
1355 deal with such files in
1362 Therefore, we have set the default value of the prefix to be the filename
1363 with all the dots being replaced by
1364 \begin_inset Quotes eld
1368 \begin_inset Quotes erd
1372 We can also solve this problem in Sweave directly: set the Sweave option
1377 to be a character string without dots, and this option will override the
1381 \begin_layout Standard
1382 We have pre-specified an option
1386 for the Sweave package.
1387 This option stops Sweave from loading these two packages:
1390 \begin_layout Standard
1394 \begin_layout Plain Layout
1401 \begin_layout Plain Layout
1405 RequirePackage[T1]{fontenc}
1408 \begin_layout Plain Layout
1415 \begin_layout Plain Layout
1427 \begin_layout Standard
1429 can deal with the font encoding by default, so there is no need to ask
1430 Sweave to do this (furthermore, this may bring clashes of package options);
1431 besides, the users can load the
1435 package by themselves in the preamble if needed.
1438 \begin_layout Section
1442 \begin_layout Standard
1443 Since \SpecialChar LyX
1444 2.0, we can see the detailed information during compilation via
1446 View\SpecialChar menuseparator
1450 This is extremely helpful for trouble shooting – the process of R will
1451 be shown in the message panel, and we will be able to know which chunk
1452 goes wrong in cases of errors.
1453 For example, when you compile this document, you can see messages like
1457 \begin_layout Standard
1461 \begin_layout Plain Layout
1468 \begin_layout Plain Layout
1470 17:58:47.868: Processing code chunks with options ...
1473 \begin_layout Plain Layout
1475 17:58:47.885: 1 : echo keep.source term verbatim (label=setup)
1478 \begin_layout Plain Layout
1480 17:58:47.889: 2 : echo keep.source (label=sweave-manual)
1483 \begin_layout Plain Layout
1485 17:58:47.908: 3 : echo keep.source term verbatim (label=print-integers)
1488 \begin_layout Plain Layout
1493 \begin_layout Plain Layout
1495 17:58:47.941: Loading required package: xtable
1498 \begin_layout Plain Layout
1500 17:58:47.976: 9 : echo term verbatim pdf (label=iris-pairs)
1503 \begin_layout Plain Layout
1508 \begin_layout Plain Layout
1510 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1513 \begin_layout Plain Layout
1525 \begin_layout Standard
1531 If any chunk causes an error in R, you will see the error message here
1535 \begin_layout Standard
1536 Another issue with the \SpecialChar TeX
1537 code environment is the Sweave code chunk may fail
1538 when we change the alignment of the chunk paragraph in \SpecialChar LyX
1543 ), because \SpecialChar LyX
1544 may add a macro like
1550 before the code chunk without adding a new line, which will lead to a code
1554 \begin_layout Standard
1558 \begin_layout Plain Layout
1565 \begin_layout Plain Layout
1569 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1572 \begin_layout Plain Layout
1584 \begin_layout Standard
1585 This is not a legitimate Sweave code chunk, since
1589 must start in a new line with nothing before it.
1590 This does not happen when using the Chunk inset instead of the \SpecialChar TeX
1596 \begin_layout Section
1600 \begin_layout Standard
1602 \change_deleted 274215730 1507313073
1604 \change_inserted 274215730 1507313078
1608 \change_deleted 274215730 1507313083
1610 \begin_inset Flex URL
1613 \begin_layout Plain Layout
1615 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1621 \change_inserted 274215730 1507313095
1623 \begin_inset Flex URL
1626 \begin_layout Plain Layout
1628 \change_inserted 274215730 1507313100
1630 https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf
1640 The CRAN task view on
1641 \begin_inset Quotes eld
1644 Reproducible Research
1645 \begin_inset Quotes erd
1648 has a list of Sweave-related packages which can be potentially useful;
1650 \begin_inset Flex URL
1653 \begin_layout Plain Layout
1655 http://cran.r-project.org/web/views/ReproducibleResearch.html
1661 Questions about \SpecialChar LyX
1662 and Sweave can be posted to the \SpecialChar LyX
1664 \begin_inset Flex URL
1667 \begin_layout Plain Layout
1669 http://www.lyx.org/MailingLists
1675 More \SpecialChar LyX
1676 examples are provided at
1677 \begin_inset Flex URL
1680 \begin_layout Plain Layout
1682 https://github.com/yihui/lyx
1690 \begin_layout Section
1694 \begin_layout Subsection
1695 How to use Sweave with beamer?
1698 \begin_layout Standard
1699 You will end up with \SpecialChar LaTeX
1700 errors if you use Sweave in normal beamer slides in
1702 ; the reason is the beamer frame that contains verbatim R code.
1703 The solution is to use a
1711 , or to pass the option
1715 to the frame title via
1717 Insert\SpecialChar menuseparator
1723 \begin_layout Subsection
1724 Can I define my own R script to compile the Rnw document?
1727 \begin_layout Standard
1729 First you need to understand how the R script
1733 /scripts/lyxsweave.R
1735 works, which was introduced previously, and we recommend you to read the
1736 R source code as well (on how R takes the paths and uses
1740 to handle the Rnw document).
1741 Then you put your customized
1755 Help\SpecialChar menuseparator
1756 About \SpecialChar LyX
1760 Note \SpecialChar LyX
1765 before going to its own
1773 is found in the user directory, it will be used.
1774 It is not recommended to hack the script under the \SpecialChar LyX
1775 library directory directly,
1776 since updating \SpecialChar LyX
1777 will override you modified script (the user directory will
1781 \begin_layout Standard
1782 Beside the support for the official Sweave in R, the
1786 package is also supported, which is an alternative tool to Sweave; the
1787 corresponding module is named