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 \SpecialChar LyX
144 \begin_layout Plain Layout
145 Department of Statistics, Iowa State University.
147 \begin_inset CommandInset href
149 name "xie@yihui.name"
150 target "xie@yihui.name"
164 \begin_layout Plain Layout
177 \begin_layout Plain Layout
189 \begin_layout Standard
190 Sweave is a tool in the R language (
191 \begin_inset Flex URL
194 \begin_layout Plain Layout
196 http://www.r-project.org
201 ) for dynamic report generation using the ideas of literate programming.
202 R code can be mixed into a \SpecialChar LaTeX
203 document, and will be executed in R when the
204 document is compiled; the results are written into the \SpecialChar TeX
208 \begin_layout Standard
209 Since \SpecialChar LyX
210 2.0, Sweave can be used out of the box with any document class using
220 module to a \SpecialChar LyX
223 Document\SpecialChar menuseparator
224 Settings\SpecialChar menuseparator
228 In this manual, we demonstrate some common features of Sweave and explain
229 how the module works.
230 A PDF version of this manual can be found at
231 \begin_inset Flex URL
234 \begin_layout Plain Layout
236 https://github.com/downloads/yihui/lyx/sweave.pdf
244 \begin_layout Section
248 \begin_layout Standard
253 module depends on the executable
257 , so it has to be in the
261 variable of the system.
262 It is usually not a problem for Linux and Mac OS users, but may be confusing
263 to Windows users; if you do not understand what is
267 under Windows, you may either use the R script
272 \begin_inset Flex URL
275 \begin_layout Plain Layout
277 https://github.com/yihui/lyx
282 to change it automatically (not recommended; you should learn
288 Tools\SpecialChar menuseparator
289 Preferences\SpecialChar menuseparator
290 Paths\SpecialChar menuseparator
293 to add the bin path of R (recommended), which is often like
305 and you can find it in R:
308 \begin_layout Standard
312 \begin_layout Plain Layout
314 <<where-is-R-bin,eval=FALSE>>=
317 \begin_layout Plain Layout
322 \begin_layout Plain Layout
332 \begin_layout Standard
333 After you have done these changes, you need to reconfigure \SpecialChar LyX
336 Tools\SpecialChar menuseparator
352 module will be unavailable.
353 It is recommended to use the latest version of R, since Sweave is still
355 Another dependency is introduced by the \SpecialChar LaTeX
360 which is shipped with R under this path (it is not on CTAN):
363 \begin_layout Standard
367 \begin_layout Plain Layout
369 <<sweave-sty,eval=FALSE>>=
372 \begin_layout Plain Layout
374 file.path(R.home('share'), 'texmf', 'tex', 'latex')
377 \begin_layout Plain Layout
387 \begin_layout Standard
389 users may add the texmf directory to the list of root directories in
391 through Mik\SpecialChar TeX
415 , it will directly copy from R, so the Sweave module works even if you do
416 not know where is this style file.
419 \begin_layout Section
423 \begin_layout Standard
424 Before we write an Sweave document, we can set some Sweave options; the
431 in Sweave and it can be inserted in a \SpecialChar LyX
434 Insert\SpecialChar menuseparator
435 Custom Insets\SpecialChar menuseparator
438 , or just use \SpecialChar TeX
441 Insert\SpecialChar menuseparator
446 For example, we can set a global option
450 in the beginning of a document to suppress all R code (by default, both
451 R code and its output will show up in the \SpecialChar LaTeX
456 \begin_layout Standard
457 To write R code in \SpecialChar LyX
458 , you can first switch to the
462 environment in the layout styles list (droplist in the top-left corner
463 in the toolbar), or you can simply open a \SpecialChar TeX
464 code environment and write Sweave
466 The latter approach is recommended since it is more stable.
469 \begin_layout Standard
470 Some R options can be useful to the Sweave output as well, e.g.
480 The former option can adjust the width of the output, and the latter can
481 avoid a common problem with quotes.
482 Users are encouraged to read the Sweave manual in R for more details.
485 \begin_layout Standard
489 \begin_layout Plain Layout
491 <<setup, keep.source=TRUE>>=
494 \begin_layout Plain Layout
496 ## the option 'digits' can set the (approximate) number of digits
499 \begin_layout Plain Layout
501 options(width = 70, useFancyQuotes = FALSE, digits = 4)
504 \begin_layout Plain Layout
509 \begin_layout Plain Layout
511 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
514 \begin_layout Plain Layout
516 ## view the Sweave vignette (requires R >= 2.13.0)
519 \begin_layout Plain Layout
521 vignette("Sweave", package = "utils")
524 \begin_layout Plain Layout
534 \begin_layout Standard
535 A simple example that will run in any S engine:
538 \begin_layout Standard
542 \begin_layout Plain Layout
544 <<print-integers, keep.source=TRUE>>=
547 \begin_layout Plain Layout
549 ## the integers from 1 to 10 are
552 \begin_layout Plain Layout
557 \begin_layout Plain Layout
562 \begin_layout Plain Layout
564 <<hide-results, results=hide>>=
567 \begin_layout Plain Layout
569 ## intentionally hide the output by setting results=hide
572 \begin_layout Plain Layout
577 \begin_layout Plain Layout
587 \begin_layout Standard
588 We can also emulate a simple calculator:
591 \begin_layout Standard
595 \begin_layout Plain Layout
600 \begin_layout Plain Layout
605 \begin_layout Plain Layout
610 \begin_layout Plain Layout
620 \begin_layout Standard
621 Now we look at some Gaussian data:
624 \begin_layout Standard
628 \begin_layout Plain Layout
633 \begin_layout Plain Layout
635 set.seed(123) # for reproducibility
638 \begin_layout Plain Layout
643 \begin_layout Plain Layout
648 \begin_layout Plain Layout
653 \begin_layout Plain Layout
663 \begin_layout Standard
664 Note that we can easily integrate some numbers into standard text; this
665 is done with the command
671 , and the corresponding \SpecialChar LyX
674 Insert\SpecialChar menuseparator
675 Custom Insets\SpecialChar menuseparator
678 , or alternatively just use \SpecialChar TeX
680 For example, the third element of the vector
685 \begin_inset Flex S/R expression
688 \begin_layout Plain Layout
701 \begin_inset Formula $p$
704 -value of the test is
705 \begin_inset Flex S/R expression
708 \begin_layout Plain Layout
710 format.pval(t1$p.value)
716 You can round these numbers using functions like
724 \begin_layout Standard
734 Now we look at a summary of the
755 \begin_layout Standard
759 \begin_layout Plain Layout
764 \begin_layout Plain Layout
769 \begin_layout Plain Layout
779 \begin_layout Standard
780 \begin_inset Float table
785 \begin_layout Plain Layout
786 \begin_inset Caption Standard
788 \begin_layout Plain Layout
789 Quantiles of the first four variables in the
794 \begin_inset CommandInset label
796 name "tab:xtable-demo"
808 \begin_layout Plain Layout
813 \begin_layout Plain Layout
817 \begin_layout Plain Layout
819 <<xtable-demo,results=tex,echo=FALSE>>=
822 \begin_layout Plain Layout
824 if (require('xtable')) {
827 \begin_layout Plain Layout
829 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
832 \begin_layout Plain Layout
834 } else cat('the xtable package is not available! please install.packages("xtable"
838 \begin_layout Plain Layout
853 \begin_layout Standard
854 Usually people do not like such
855 \begin_inset Quotes eld
859 \begin_inset Quotes erd
862 output, and we can polish these messy numbers with the
868 \begin_inset CommandInset ref
870 reference "tab:xtable-demo"
874 is an example demonstrating how to use the
878 package with Sweave; note the chunk option
882 makes sure that the R output is written out as raw \SpecialChar LaTeX
883 code instead of being
884 wrapped in a special environment (
891 \begin_layout Standard
893 \begin_inset CommandInset ref
895 reference "fig:iris-pairs"
900 \begin_inset CommandInset ref
902 reference "fig:iris-boxplots"
906 are two simple examples of producing graphics output with Sweave.
909 \begin_layout Standard
910 \begin_inset Float figure
915 \begin_layout Plain Layout
917 \begin_inset Note Note
920 \begin_layout Plain Layout
921 Both eps and pdf are set to true so that both latex and pdflatex work.
922 In practice, you would probably just choose one.
931 \begin_layout Plain Layout
933 <<iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE>>=
936 \begin_layout Plain Layout
938 pairs(iris, col = iris$Species)
941 \begin_layout Plain Layout
951 \begin_layout Plain Layout
952 \begin_inset Caption Standard
954 \begin_layout Plain Layout
955 Scatter plot matrix of the
960 \begin_inset CommandInset label
962 name "fig:iris-pairs"
979 \begin_layout Standard
980 \begin_inset Float figure
985 \begin_layout Plain Layout
990 \begin_layout Plain Layout
992 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T>>=
995 \begin_layout Plain Layout
997 par(mar=c(4,5,.1,.3), las=1)
1000 \begin_layout Plain Layout
1002 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1005 \begin_layout Plain Layout
1015 \begin_layout Plain Layout
1016 \begin_inset Caption Standard
1018 \begin_layout Plain Layout
1019 Boxplots of sepal length grouped by species.
1020 \begin_inset CommandInset label
1022 name "fig:iris-boxplots"
1039 \begin_layout Standard
1040 We can also export all the R code in an Sweave document as a single R script
1043 File\SpecialChar menuseparator
1044 Export\SpecialChar menuseparator
1047 ), which is done via
1054 \begin_layout Section
1062 \begin_layout Standard
1067 module declares that a \SpecialChar LyX
1068 document contains literate programming components
1069 with R, so that \SpecialChar LyX
1070 will handle such documents according to the specific converter
1071 s (see the Customization manual).
1080 converter, which essentially consists of an R script
1088 directory of \SpecialChar LyX
1090 This script takes an
1094 document from \SpecialChar LyX
1095 and call Sweave to process it to a \SpecialChar TeX
1096 document, then \SpecialChar LyX
1098 care of the rest of work.
1101 \begin_layout Standard
1102 The converter from the
1106 format to \SpecialChar LaTeX
1107 is a call to R via the command line as:
1110 \begin_layout Standard
1114 \begin_layout Plain Layout
1121 \begin_layout Plain Layout
1123 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1127 \begin_layout Plain Layout
1139 \begin_layout Standard
1140 The \SpecialChar LyX
1145 can be found from the menu
1147 Help\SpecialChar menuseparator
1148 About \SpecialChar LyX
1152 All the rest of options passed to R are explained in the R script, and
1153 the \SpecialChar LyX
1154 Customization manual also has detailed explanations about
1171 General users do not need to know clearly what they mean, but here we need
1172 to explain a few issues in the R code:
1175 \begin_layout Enumerate
1176 the encoding string of the \SpecialChar LyX
1177 document is passed to R as an command line option;
1187 The encoding is used for R to read the
1194 \begin_layout Enumerate
1195 the R code chunks in the Sweave document are executed under the directory
1196 of the \SpecialChar LyX
1197 document (if you are not sure, put
1201 in a code chunk to see what is the current working directory).
1202 In this case, you can put data files under the same directory and use relative
1203 paths in R to guarantee reproducibility, i.e.
1204 we do not need to write hard-coded paths which may not exist in other systems.
1207 \begin_layout Enumerate
1208 if \SpecialChar LaTeX
1213 , this R script will copy it to the temporary directory automatically where
1214 the \SpecialChar LaTeX
1215 compilation takes place.
1218 \begin_layout Enumerate
1219 Sweave will use the filename of the Rnw file as the prefix string for the
1220 graphics output by default; one known issue (
1221 \begin_inset CommandInset href
1224 target "http://www.lyx.org/trac/ticket/7551"
1228 ) is that if the filename (without extension) contains dots, \SpecialChar LaTeX
1230 deal with such files in
1237 Therefore, we have set the default value of the prefix to be the filename
1238 with all the dots being replaced by
1239 \begin_inset Quotes eld
1243 \begin_inset Quotes erd
1247 We can also solve this problem in Sweave directly: set the Sweave option
1252 to be a character string without dots, and this option will override the
1256 \begin_layout Standard
1257 We have pre-specified an option
1261 for the Sweave package.
1262 This option stops Sweave from loading these two packages:
1265 \begin_layout Standard
1269 \begin_layout Plain Layout
1276 \begin_layout Plain Layout
1280 RequirePackage[T1]{fontenc}
1283 \begin_layout Plain Layout
1290 \begin_layout Plain Layout
1302 \begin_layout Standard
1304 can deal with the font encoding by default, so there is no need to ask
1305 Sweave to do this (furthermore, this may bring clashes of package options);
1306 besides, the users can load the
1310 package by themselves in the preamble if needed.
1313 \begin_layout Section
1317 \begin_layout Standard
1318 Since \SpecialChar LyX
1319 2.0, we can see the detailed information during compilation via
1321 View\SpecialChar menuseparator
1325 This is extremely helpful for trouble shooting – 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 \SpecialChar LyX
1417 ), because \SpecialChar 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 \SpecialChar LyX
1524 and Sweave can be posted to the \SpecialChar LyX
1526 \begin_inset Flex URL
1529 \begin_layout Plain Layout
1531 http://www.lyx.org/MailingLists
1537 More \SpecialChar 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 \SpecialChar LaTeX
1562 errors if you use Sweave in normal beamer slides in
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 \SpecialChar LyX
1622 Note \SpecialChar LyX
1627 before going to its own
1635 is found in the user directory, it will be used.
1636 It is not recommended to hack the script under the \SpecialChar LyX
1637 library directory directly,
1638 since updating \SpecialChar LyX
1639 will override you modified script (the user directory will
1643 \begin_layout Standard
1644 Beside the support for the official Sweave in R, the
1648 package is also supported, which is an alternative tool to Sweave; the
1649 corresponding module is named