1 #LyX 2.1 created this file. For more info see http://www.lyx.org/
7 %% maxwidth is the original width if it's less than linewidth
8 %% otherwise use linewidth (to make sure the graphics do not exceed the margin)
10 \ifdim\Gin@nat@width>\linewidth
18 \use_default_options true
22 \maintain_unincluded_children false
24 \language_package auto
25 \inputencoding default
31 \font_default_family default
32 \use_non_tex_fonts false
38 \default_output_format default
40 \bibtex_command default
41 \index_command default
42 \paperfontsize default
47 \use_package amsmath 1
48 \use_package amssymb 1
50 \use_package mathdots 1
51 \use_package mathtools 0
53 \use_package undertilde 0
55 \cite_engine_type numerical
59 \paperorientation portrait
73 \paragraph_separation indent
74 \paragraph_indentation default
75 \quotes_language english
78 \paperpagestyle default
79 \tracking_changes false
88 \begin_layout Standard
92 \begin_layout Plain Layout
96 SweaveOpts{prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE}
104 \begin_layout Standard
108 \begin_layout Plain Layout
112 maxwidth was defined in the preamble; see document settings
115 \begin_layout Plain Layout
130 Using Sweave with LyX
138 \begin_layout Plain Layout
139 Department of Statistics, Iowa State University.
141 \begin_inset CommandInset href
143 name "xie@yihui.name"
144 target "xie@yihui.name"
158 \begin_layout Plain Layout
171 \begin_layout Plain Layout
183 \begin_layout Standard
184 Sweave is a tool in the R language (
185 \begin_inset Flex URL
188 \begin_layout Plain Layout
190 http://www.r-project.org
195 ) for dynamic report generation using the ideas of literate programming.
196 R code can be mixed into a LaTeX document, and will be executed in R when
197 the document is compiled; the results are written into the TeX output.
200 \begin_layout Standard
201 Since LyX 2.0, Sweave can be used out of the box with any document class
211 module to a LyX document from
213 Document\SpecialChar \menuseparator
214 Settings\SpecialChar \menuseparator
218 In this manual, we demonstrate some common features of Sweave and explain
219 how the module works.
220 A PDF version of this manual can be found at
221 \begin_inset Flex URL
224 \begin_layout Plain Layout
226 https://github.com/downloads/yihui/lyx/sweave.pdf
234 \begin_layout Section
238 \begin_layout Standard
243 module depends on the executable
247 , so it has to be in the
251 variable of the system.
252 It is usually not a problem for Linux and Mac OS users, but may be confusing
253 to Windows users; if you do not understand what is
257 under Windows, you may either use the R script
262 \begin_inset Flex URL
265 \begin_layout Plain Layout
267 https://github.com/yihui/lyx
272 to change it automatically (not recommended; you should learn
278 Tools\SpecialChar \menuseparator
279 Preferences\SpecialChar \menuseparator
280 Paths\SpecialChar \menuseparator
283 to add the bin path of R (recommended), which is often like
295 and you can find it in R:
298 \begin_layout Standard
302 \begin_layout Plain Layout
304 <<where-is-R-bin,eval=FALSE>>=
307 \begin_layout Plain Layout
312 \begin_layout Plain Layout
322 \begin_layout Standard
323 After you have done these changes, you need to reconfigure LyX by
325 Tools\SpecialChar \menuseparator
341 module will be unavailable.
342 It is recommended to use the latest version of R, since Sweave is still
344 Another dependency is introduced by the LaTeX style file
348 which is shipped with R under this path (it is not on CTAN):
351 \begin_layout Standard
355 \begin_layout Plain Layout
357 <<sweave-sty,eval=FALSE>>=
360 \begin_layout Plain Layout
362 file.path(R.home('share'), 'texmf', 'tex', 'latex')
365 \begin_layout Plain Layout
375 \begin_layout Standard
376 MikTeX users may add the texmf directory to the list of root directories
377 in MikTeX through MikTeX settings.
399 , it will directly copy from R, so the Sweave module works even if you do
400 not know where is this style file.
403 \begin_layout Section
407 \begin_layout Standard
408 Before we write an Sweave document, we can set some Sweave options; the
415 in Sweave and it can be inserted in a LyX document by
417 Insert\SpecialChar \menuseparator
418 Custom Insets\SpecialChar \menuseparator
421 , or just use TeX code to write it (
423 Insert\SpecialChar \menuseparator
427 For example, we can set a global option
431 in the beginning of a document to suppress all R code (by default, both
432 R code and its output will show up in the LaTeX document).
436 \begin_layout Standard
437 To write R code in LyX, you can first switch to the
441 environment in the layout styles list (droplist in the top-left corner
442 in the toolbar), or you can simply open a TeX code environment and write
443 Sweave code chunks in it.
444 The latter approach is recommended since it is more stable.
447 \begin_layout Standard
448 Some R options can be useful to the Sweave output as well, e.g.
458 The former option can adjust the width of the output, and the latter can
459 avoid a common problem with quotes.
460 Users are encouraged to read the Sweave manual in R for more details.
463 \begin_layout Standard
467 \begin_layout Plain Layout
469 <<setup, keep.source=TRUE>>=
472 \begin_layout Plain Layout
474 ## the option 'digits' can set the (approximate) number of digits
477 \begin_layout Plain Layout
479 options(width = 70, useFancyQuotes = FALSE, digits = 4)
482 \begin_layout Plain Layout
487 \begin_layout Plain Layout
489 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
492 \begin_layout Plain Layout
494 ## view the Sweave vignette (requires R >= 2.13.0)
497 \begin_layout Plain Layout
499 vignette("Sweave", package = "utils")
502 \begin_layout Plain Layout
512 \begin_layout Standard
513 A simple example that will run in any S engine:
516 \begin_layout Standard
520 \begin_layout Plain Layout
522 <<print-integers, keep.source=TRUE>>=
525 \begin_layout Plain Layout
527 ## the integers from 1 to 10 are
530 \begin_layout Plain Layout
535 \begin_layout Plain Layout
540 \begin_layout Plain Layout
542 <<hide-results, results=hide>>=
545 \begin_layout Plain Layout
547 ## intentionally hide the output by setting results=hide
550 \begin_layout Plain Layout
555 \begin_layout Plain Layout
565 \begin_layout Standard
566 We can also emulate a simple calculator:
569 \begin_layout Standard
573 \begin_layout Plain Layout
578 \begin_layout Plain Layout
583 \begin_layout Plain Layout
588 \begin_layout Plain Layout
598 \begin_layout Standard
599 Now we look at some Gaussian data:
602 \begin_layout Standard
606 \begin_layout Plain Layout
611 \begin_layout Plain Layout
613 set.seed(123) # for reproducibility
616 \begin_layout Plain Layout
621 \begin_layout Plain Layout
626 \begin_layout Plain Layout
631 \begin_layout Plain Layout
641 \begin_layout Standard
642 Note that we can easily integrate some numbers into standard text; this
643 is done with the command
649 , and the corresponding LyX menu is
651 Insert\SpecialChar \menuseparator
652 Custom Insets\SpecialChar \menuseparator
655 , or alternatively just use TeX code.
656 For example, the third element of the vector
661 \begin_inset Flex S/R expression
664 \begin_layout Plain Layout
677 \begin_inset Formula $p$
680 -value of the test is
681 \begin_inset Flex S/R expression
684 \begin_layout Plain Layout
686 format.pval(t1$p.value)
692 You can round these numbers using functions like
700 \begin_layout Standard
710 Now we look at a summary of the
731 \begin_layout Standard
735 \begin_layout Plain Layout
740 \begin_layout Plain Layout
745 \begin_layout Plain Layout
755 \begin_layout Standard
756 \begin_inset Float table
761 \begin_layout Plain Layout
764 \begin_layout Plain Layout
765 Quantiles of the first four variables in the
770 \begin_inset CommandInset label
772 name "tab:xtable-demo"
784 \begin_layout Plain Layout
789 \begin_layout Plain Layout
793 \begin_layout Plain Layout
795 <<xtable-demo,results=tex,echo=FALSE>>=
798 \begin_layout Plain Layout
800 if (require('xtable')) {
803 \begin_layout Plain Layout
805 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
808 \begin_layout Plain Layout
810 } else cat('the xtable package is not available! please install.packages("xtable"
814 \begin_layout Plain Layout
829 \begin_layout Standard
830 Usually people do not like such
831 \begin_inset Quotes eld
835 \begin_inset Quotes erd
838 output, and we can polish these messy numbers with the
844 \begin_inset CommandInset ref
846 reference "tab:xtable-demo"
850 is an example demonstrating how to use the
854 package with Sweave; note the chunk option
858 makes sure that the R output is written out as raw LaTeX code instead of
859 being wrapped in a special environment (
866 \begin_layout Standard
868 \begin_inset CommandInset ref
870 reference "fig:iris-pairs"
875 \begin_inset CommandInset ref
877 reference "fig:iris-boxplots"
881 are two simple examples of producing graphics output with Sweave.
884 \begin_layout Standard
885 \begin_inset Float figure
890 \begin_layout Plain Layout
895 \begin_layout Plain Layout
897 <<iris-pairs,fig=TRUE,width=4.5,height=4.5>>=
900 \begin_layout Plain Layout
902 pairs(iris, col = iris$Species)
905 \begin_layout Plain Layout
915 \begin_layout Plain Layout
918 \begin_layout Plain Layout
919 Scatter plot matrix of the
924 \begin_inset CommandInset label
926 name "fig:iris-pairs"
943 \begin_layout Standard
944 \begin_inset Float figure
949 \begin_layout Plain Layout
954 \begin_layout Plain Layout
956 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F>>=
959 \begin_layout Plain Layout
961 par(mar=c(4,5,.1,.3), las=1)
964 \begin_layout Plain Layout
966 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
969 \begin_layout Plain Layout
979 \begin_layout Plain Layout
982 \begin_layout Plain Layout
983 Boxplots of sepal length grouped by species.
984 \begin_inset CommandInset label
986 name "fig:iris-boxplots"
1003 \begin_layout Standard
1004 We can also export all the R code in an Sweave document as a single R script
1007 File\SpecialChar \menuseparator
1008 Export\SpecialChar \menuseparator
1011 ), which is done via
1018 \begin_layout Section
1026 \begin_layout Standard
1031 module declares that a LyX document contains literate programming components
1032 with R, so that LyX will handle such documents according to the specific
1033 converters (see the Customization manual).
1042 converter, which essentially consists of an R script
1051 This script takes an
1055 document from LyX and call Sweave to process it to a TeX document, then
1056 LyX takes care of the rest of work.
1059 \begin_layout Standard
1060 The converter from the
1064 format to LaTeX is a call to R via the command line as:
1067 \begin_layout Standard
1071 \begin_layout Plain Layout
1078 \begin_layout Plain Layout
1080 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1084 \begin_layout Plain Layout
1096 \begin_layout Standard
1097 The LyX library directory
1101 can be found from the menu
1103 Help\SpecialChar \menuseparator
1107 All the rest of options passed to R are explained in the R script, and
1108 the LyX Customization manual also has detailed explanations about
1125 General users do not need to know clearly what they mean, but here we need
1126 to explain a few issues in the R code:
1129 \begin_layout Enumerate
1130 the encoding string of the LyX document is passed to R as an command line
1131 option; possible values are
1140 The encoding is used for R to read the
1147 \begin_layout Enumerate
1148 the R code chunks in the Sweave document are executed under the directory
1149 of the LyX document (if you are not sure, put
1153 in a code chunk to see what is the current working directory).
1154 In this case, you can put data files under the same directory and use relative
1155 paths in R to guarantee reproducibility, i.e.
1156 we do not need to write hard-coded paths which may not exist in other systems.
1159 \begin_layout Enumerate
1160 if LaTeX cannot find
1164 , this R script will copy it to the temporary directory automatically where
1165 the LaTeX compilation takes place.
1168 \begin_layout Enumerate
1169 Sweave will use the filename of the Rnw file as the prefix string for the
1170 graphics output by default; one known issue (
1171 \begin_inset CommandInset href
1174 target "http://www.lyx.org/trac/ticket/7551"
1178 ) is that if the filename (without extension) contains dots, LaTeX can fail
1179 to deal with such files in
1186 Therefore, we have set the default value of the prefix to be the filename
1187 with all the dots being replaced by
1188 \begin_inset Quotes eld
1192 \begin_inset Quotes erd
1196 We can also solve this problem in Sweave directly: set the Sweave option
1201 to be a character string without dots, and this option will override the
1205 \begin_layout Standard
1206 We have pre-specified an option
1210 for the Sweave package.
1211 This option stops Sweave from loading these two packages:
1214 \begin_layout Standard
1218 \begin_layout Plain Layout
1225 \begin_layout Plain Layout
1229 RequirePackage[T1]{fontenc}
1232 \begin_layout Plain Layout
1239 \begin_layout Plain Layout
1251 \begin_layout Standard
1252 LyX can deal with the font encoding by default, so there is no need to ask
1253 Sweave to do this (furthermore, this may bring clashes of package options);
1254 besides, the users can load the
1258 package by themselves in the preamble if needed.
1261 \begin_layout Section
1265 \begin_layout Standard
1266 Since LyX 2.0, we can see the detailed information during compilation via
1269 View\SpecialChar \menuseparator
1273 This is extremely helpful for trouble shooting -- the process of R will
1274 be shown in the message panel, and we will be able to know which chunk
1275 goes wrong in cases of errors.
1276 For example, when you compile this document, you can see messages like
1280 \begin_layout Standard
1284 \begin_layout Plain Layout
1291 \begin_layout Plain Layout
1293 17:58:47.868: Processing code chunks with options ...
1296 \begin_layout Plain Layout
1298 17:58:47.885: 1 : echo keep.source term verbatim (label=setup)
1301 \begin_layout Plain Layout
1303 17:58:47.889: 2 : echo keep.source (label=sweave-manual)
1306 \begin_layout Plain Layout
1308 17:58:47.908: 3 : echo keep.source term verbatim (label=print-integers)
1311 \begin_layout Plain Layout
1316 \begin_layout Plain Layout
1318 17:58:47.941: Loading required package: xtable
1321 \begin_layout Plain Layout
1323 17:58:47.976: 9 : echo term verbatim pdf (label=iris-pairs)
1326 \begin_layout Plain Layout
1331 \begin_layout Plain Layout
1333 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1336 \begin_layout Plain Layout
1348 \begin_layout Standard
1354 If any chunk causes an error in R, you will see the error message here
1358 \begin_layout Standard
1359 Another known issue is the Sweave code chunk may fail when we change the
1360 alignment of the chunk paragraph in LyX (using
1364 ), because LyX may add a macro like
1370 before the code chunk without adding a new line, which will lead to a code
1374 \begin_layout Standard
1378 \begin_layout Plain Layout
1385 \begin_layout Plain Layout
1389 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1392 \begin_layout Plain Layout
1404 \begin_layout Standard
1405 This is not a legitimate Sweave code chunk, since
1409 must start in a new line with nothing before it.
1410 Therefore we recommend you to double check the real source code via
1412 View\SpecialChar \menuseparator
1415 when changing the alignment of a chunk paragraph, and make sure
1419 appears in a new line.
1420 This explains why we manually added an empty line in the code chunk in
1422 \begin_inset CommandInset ref
1424 reference "tab:xtable-demo"
1431 \begin_layout Section
1435 \begin_layout Standard
1436 The homepage of Sweave is at
1437 \begin_inset Flex URL
1440 \begin_layout Plain Layout
1442 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1448 The CRAN task view on
1449 \begin_inset Quotes eld
1452 Reproducible Research
1453 \begin_inset Quotes erd
1456 has a list of Sweave-related packages which can be potentially useful;
1458 \begin_inset Flex URL
1461 \begin_layout Plain Layout
1463 http://cran.r-project.org/web/views/ReproducibleResearch.html
1469 Questions about LyX and Sweave can be posted to the LyX mailing list (
1470 \begin_inset Flex URL
1473 \begin_layout Plain Layout
1475 http://www.lyx.org/MailingLists
1481 More LyX examples are provided at
1482 \begin_inset Flex URL
1485 \begin_layout Plain Layout
1487 https://github.com/yihui/lyx
1495 \begin_layout Section
1499 \begin_layout Subsection
1500 How to use Sweave with beamer?
1503 \begin_layout Standard
1504 You will end up with LaTeX errors if you use Sweave in beamer slides in
1505 LyX; the reason is the beamer frame that contains verbatim R code and output
1511 However, currently there is no straightforward way to do this.
1512 One way is to write raw TeX code, and the other is to use the
1514 beamer-fragile.module
1517 \begin_inset CommandInset href
1520 target "http://www.lyx.org/trac/ticket/7273"
1524 (put this file under the
1528 directory of LyX user directory and reconfigure LyX).
1529 After including this module in your document, you should be able to use
1532 Insert\SpecialChar \menuseparator
1533 Custom Insets\SpecialChar \menuseparator
1536 , which is essentially
1540 begin{frame}[fragile]
1545 \begin_layout Subsection
1546 Can I define my own R script to compile the Rnw document?
1549 \begin_layout Standard
1551 First you need to understand how the R script
1555 /scripts/lyxsweave.R
1557 works, which was introduced previously, and we recommend you to read the
1558 R source code as well (on how R takes the paths and uses
1562 to handle the Rnw document).
1563 Then you put your customized
1577 Help\SpecialChar \menuseparator
1581 Note LyX will check the
1585 before going to its own
1593 is found in the user directory, it will be used.
1594 It is not recommended to hack the script under the LyX library directory
1595 directly, since updating LyX will override you modified script (the user
1596 directory will not be affected).
1599 \begin_layout Standard
1600 Beside the support for the official Sweave in R, the
1604 package is also supported, which is an alternative tool to Sweave; the
1605 corresponding module is named