1 #LyX 2.2 created this file. For more info see http://www.lyx.org/
5 \origin /systemlyxdir/examples/
8 %% maxwidth is the original width if it's less than linewidth
9 %% otherwise use linewidth (to make sure the graphics do not exceed the margin)
11 \ifdim\Gin@nat@width>\linewidth
19 \use_default_options true
23 \maintain_unincluded_children false
25 \language_package auto
26 \inputencoding default
28 \font_roman "palatino" "default"
29 \font_sans "lmss" "default"
30 \font_typewriter "lmtt" "default"
31 \font_math "auto" "auto"
32 \font_default_family default
33 \use_non_tex_fonts false
36 \font_sf_scale 100 100
37 \font_tt_scale 100 100
39 \default_output_format default
41 \bibtex_command default
42 \index_command default
43 \paperfontsize default
48 \use_package amsmath 1
49 \use_package amssymb 1
52 \use_package mathdots 1
53 \use_package mathtools 0
55 \use_package stackrel 0
56 \use_package stmaryrd 0
57 \use_package undertilde 0
59 \cite_engine_type default
63 \paperorientation portrait
77 \paragraph_separation indent
78 \paragraph_indentation default
79 \quotes_language english
82 \paperpagestyle default
83 \tracking_changes false
92 \begin_layout Standard
96 \begin_layout Plain Layout
100 SweaveOpts{prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE}
108 \begin_layout Standard
112 \begin_layout Plain Layout
116 maxwidth was defined in the preamble; see document settings
119 \begin_layout Plain Layout
134 Using Sweave with \SpecialCharNoPassThru LyX
142 \begin_layout Plain Layout
143 Department of Statistics, Iowa State University.
145 \begin_inset CommandInset href
147 name "xie@yihui.name"
148 target "xie@yihui.name"
162 \begin_layout Plain Layout
175 \begin_layout Plain Layout
187 \begin_layout Standard
188 Sweave is a tool in the R language (
189 \begin_inset Flex URL
192 \begin_layout Plain Layout
194 http://www.r-project.org
199 ) for dynamic report generation using the ideas of literate programming.
200 R code can be mixed into a \SpecialCharNoPassThru LaTeX
201 document, and will be executed in R when
202 the document is compiled; the results are written into the \SpecialCharNoPassThru TeX
206 \begin_layout Standard
207 Since \SpecialCharNoPassThru LyX
208 2.0, Sweave can be used out of the box with any document class
218 module to a \SpecialCharNoPassThru LyX
221 Document\SpecialChar menuseparator
222 Settings\SpecialChar menuseparator
226 In this manual, we demonstrate some common features of Sweave and explain
227 how the module works.
228 A PDF version of this manual can be found at
229 \begin_inset Flex URL
232 \begin_layout Plain Layout
234 https://github.com/downloads/yihui/lyx/sweave.pdf
242 \begin_layout Section
246 \begin_layout Standard
251 module depends on the executable
255 , so it has to be in the
259 variable of the system.
260 It is usually not a problem for Linux and Mac OS users, but may be confusing
261 to Windows users; if you do not understand what is
265 under Windows, you may either use the R script
270 \begin_inset Flex URL
273 \begin_layout Plain Layout
275 https://github.com/yihui/lyx
280 to change it automatically (not recommended; you should learn
286 Tools\SpecialChar menuseparator
287 Preferences\SpecialChar menuseparator
288 Paths\SpecialChar menuseparator
291 to add the bin path of R (recommended), which is often like
303 and you can find it in R:
306 \begin_layout Standard
310 \begin_layout Plain Layout
312 <<where-is-R-bin,eval=FALSE>>=
315 \begin_layout Plain Layout
320 \begin_layout Plain Layout
330 \begin_layout Standard
331 After you have done these changes, you need to reconfigure \SpecialCharNoPassThru LyX
334 Tools\SpecialChar menuseparator
350 module will be unavailable.
351 It is recommended to use the latest version of R, since Sweave is still
353 Another dependency is introduced by the \SpecialCharNoPassThru LaTeX
358 which is shipped with R under this path (it is not on CTAN):
361 \begin_layout Standard
365 \begin_layout Plain Layout
367 <<sweave-sty,eval=FALSE>>=
370 \begin_layout Plain Layout
372 file.path(R.home('share'), 'texmf', 'tex', 'latex')
375 \begin_layout Plain Layout
385 \begin_layout Standard
386 Mik\SpecialCharNoPassThru TeX
387 users may add the texmf directory to the list of root directories
388 in Mik\SpecialCharNoPassThru TeX
389 through Mik\SpecialCharNoPassThru TeX
408 If \SpecialCharNoPassThru LyX
413 , it will directly copy from R, so the Sweave module works even if you do
414 not know where is this style file.
417 \begin_layout Section
421 \begin_layout Standard
422 Before we write an Sweave document, we can set some Sweave options; the
429 in Sweave and it can be inserted in a \SpecialCharNoPassThru LyX
432 Insert\SpecialChar menuseparator
433 Custom Insets\SpecialChar menuseparator
436 , or just use \SpecialCharNoPassThru TeX
439 Insert\SpecialChar menuseparator
440 \SpecialCharNoPassThru TeX
444 For example, we can set a global option
448 in the beginning of a document to suppress all R code (by default, both
449 R code and its output will show up in the \SpecialCharNoPassThru LaTeX
454 \begin_layout Standard
455 To write R code in \SpecialCharNoPassThru LyX
456 , you can first switch to the
460 environment in the layout styles list (droplist in the top-left corner
461 in the toolbar), or you can simply open a \SpecialCharNoPassThru TeX
462 code environment and write
463 Sweave code chunks in it.
464 The latter approach is recommended since it is more stable.
467 \begin_layout Standard
468 Some R options can be useful to the Sweave output as well, e.g.
478 The former option can adjust the width of the output, and the latter can
479 avoid a common problem with quotes.
480 Users are encouraged to read the Sweave manual in R for more details.
483 \begin_layout Standard
487 \begin_layout Plain Layout
489 <<setup, keep.source=TRUE>>=
492 \begin_layout Plain Layout
494 ## the option 'digits' can set the (approximate) number of digits
497 \begin_layout Plain Layout
499 options(width = 70, useFancyQuotes = FALSE, digits = 4)
502 \begin_layout Plain Layout
507 \begin_layout Plain Layout
509 <<sweave-manual, eval=FALSE, keep.source=TRUE>>=
512 \begin_layout Plain Layout
514 ## view the Sweave vignette (requires R >= 2.13.0)
517 \begin_layout Plain Layout
519 vignette("Sweave", package = "utils")
522 \begin_layout Plain Layout
532 \begin_layout Standard
533 A simple example that will run in any S engine:
536 \begin_layout Standard
540 \begin_layout Plain Layout
542 <<print-integers, keep.source=TRUE>>=
545 \begin_layout Plain Layout
547 ## the integers from 1 to 10 are
550 \begin_layout Plain Layout
555 \begin_layout Plain Layout
560 \begin_layout Plain Layout
562 <<hide-results, results=hide>>=
565 \begin_layout Plain Layout
567 ## intentionally hide the output by setting results=hide
570 \begin_layout Plain Layout
575 \begin_layout Plain Layout
585 \begin_layout Standard
586 We can also emulate a simple calculator:
589 \begin_layout Standard
593 \begin_layout Plain Layout
598 \begin_layout Plain Layout
603 \begin_layout Plain Layout
608 \begin_layout Plain Layout
618 \begin_layout Standard
619 Now we look at some Gaussian data:
622 \begin_layout Standard
626 \begin_layout Plain Layout
631 \begin_layout Plain Layout
633 set.seed(123) # for reproducibility
636 \begin_layout Plain Layout
641 \begin_layout Plain Layout
646 \begin_layout Plain Layout
651 \begin_layout Plain Layout
661 \begin_layout Standard
662 Note that we can easily integrate some numbers into standard text; this
663 is done with the command
669 , and the corresponding \SpecialCharNoPassThru LyX
672 Insert\SpecialChar menuseparator
673 Custom Insets\SpecialChar menuseparator
676 , or alternatively just use \SpecialCharNoPassThru TeX
678 For example, the third element of the vector
683 \begin_inset Flex S/R expression
686 \begin_layout Plain Layout
699 \begin_inset Formula $p$
702 -value of the test is
703 \begin_inset Flex S/R expression
706 \begin_layout Plain Layout
708 format.pval(t1$p.value)
714 You can round these numbers using functions like
722 \begin_layout Standard
732 Now we look at a summary of the
753 \begin_layout Standard
757 \begin_layout Plain Layout
762 \begin_layout Plain Layout
767 \begin_layout Plain Layout
777 \begin_layout Standard
778 \begin_inset Float table
783 \begin_layout Plain Layout
784 \begin_inset Caption Standard
786 \begin_layout Plain Layout
787 Quantiles of the first four variables in the
792 \begin_inset CommandInset label
794 name "tab:xtable-demo"
806 \begin_layout Plain Layout
811 \begin_layout Plain Layout
815 \begin_layout Plain Layout
817 <<xtable-demo,results=tex,echo=FALSE>>=
820 \begin_layout Plain Layout
822 if (require('xtable')) {
825 \begin_layout Plain Layout
827 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
830 \begin_layout Plain Layout
832 } else cat('the xtable package is not available! please install.packages("xtable"
836 \begin_layout Plain Layout
851 \begin_layout Standard
852 Usually people do not like such
853 \begin_inset Quotes eld
857 \begin_inset Quotes erd
860 output, and we can polish these messy numbers with the
866 \begin_inset CommandInset ref
868 reference "tab:xtable-demo"
872 is an example demonstrating how to use the
876 package with Sweave; note the chunk option
880 makes sure that the R output is written out as raw \SpecialCharNoPassThru LaTeX
882 being wrapped in a special environment (
889 \begin_layout Standard
891 \begin_inset CommandInset ref
893 reference "fig:iris-pairs"
898 \begin_inset CommandInset ref
900 reference "fig:iris-boxplots"
904 are two simple examples of producing graphics output with Sweave.
907 \begin_layout Standard
908 \begin_inset Float figure
913 \begin_layout Plain Layout
915 \begin_inset Note Note
918 \begin_layout Plain Layout
919 Both eps and pdf are set to true so that both latex and pdflatex work.
920 In practice, you would probably just choose one.
929 \begin_layout Plain Layout
931 <<iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE>>=
934 \begin_layout Plain Layout
936 pairs(iris, col = iris$Species)
939 \begin_layout Plain Layout
949 \begin_layout Plain Layout
950 \begin_inset Caption Standard
952 \begin_layout Plain Layout
953 Scatter plot matrix of the
958 \begin_inset CommandInset label
960 name "fig:iris-pairs"
977 \begin_layout Standard
978 \begin_inset Float figure
983 \begin_layout Plain Layout
988 \begin_layout Plain Layout
990 <<iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T>>=
993 \begin_layout Plain Layout
995 par(mar=c(4,5,.1,.3), las=1)
998 \begin_layout Plain Layout
1000 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1003 \begin_layout Plain Layout
1013 \begin_layout Plain Layout
1014 \begin_inset Caption Standard
1016 \begin_layout Plain Layout
1017 Boxplots of sepal length grouped by species.
1018 \begin_inset CommandInset label
1020 name "fig:iris-boxplots"
1037 \begin_layout Standard
1038 We can also export all the R code in an Sweave document as a single R script
1041 File\SpecialChar menuseparator
1042 Export\SpecialChar menuseparator
1045 ), which is done via
1052 \begin_layout Section
1060 \begin_layout Standard
1065 module declares that a \SpecialCharNoPassThru LyX
1066 document contains literate programming components
1067 with R, so that \SpecialCharNoPassThru LyX
1068 will handle such documents according to the specific
1069 converters (see the Customization manual).
1078 converter, which essentially consists of an R script
1086 directory of \SpecialCharNoPassThru LyX
1088 This script takes an
1092 document from \SpecialCharNoPassThru LyX
1093 and call Sweave to process it to a \SpecialCharNoPassThru TeX
1095 \SpecialCharNoPassThru LyX
1096 takes care of the rest of work.
1099 \begin_layout Standard
1100 The converter from the
1104 format to \SpecialCharNoPassThru LaTeX
1105 is a call to R via the command line as:
1108 \begin_layout Standard
1112 \begin_layout Plain Layout
1119 \begin_layout Plain Layout
1121 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1125 \begin_layout Plain Layout
1137 \begin_layout Standard
1138 The \SpecialCharNoPassThru LyX
1143 can be found from the menu
1145 Help\SpecialChar menuseparator
1146 About \SpecialCharNoPassThru LyX
1149 All the rest of options passed to R are explained in the R script, and
1150 the \SpecialCharNoPassThru LyX
1151 Customization manual also has detailed explanations about
1168 General users do not need to know clearly what they mean, but here we need
1169 to explain a few issues in the R code:
1172 \begin_layout Enumerate
1173 the encoding string of the \SpecialCharNoPassThru LyX
1174 document is passed to R as an command line
1175 option; possible values are
1184 The encoding is used for R to read the
1191 \begin_layout Enumerate
1192 the R code chunks in the Sweave document are executed under the directory
1193 of the \SpecialCharNoPassThru LyX
1194 document (if you are not sure, put
1198 in a code chunk to see what is the current working directory).
1199 In this case, you can put data files under the same directory and use relative
1200 paths in R to guarantee reproducibility, i.e.
1201 we do not need to write hard-coded paths which may not exist in other systems.
1204 \begin_layout Enumerate
1205 if \SpecialCharNoPassThru LaTeX
1210 , this R script will copy it to the temporary directory automatically where
1211 the \SpecialCharNoPassThru LaTeX
1212 compilation takes place.
1215 \begin_layout Enumerate
1216 Sweave will use the filename of the Rnw file as the prefix string for the
1217 graphics output by default; one known issue (
1218 \begin_inset CommandInset href
1221 target "http://www.lyx.org/trac/ticket/7551"
1225 ) is that if the filename (without extension) contains dots, \SpecialCharNoPassThru LaTeX
1227 to deal with such files in
1234 Therefore, we have set the default value of the prefix to be the filename
1235 with all the dots being replaced by
1236 \begin_inset Quotes eld
1240 \begin_inset Quotes erd
1244 We can also solve this problem in Sweave directly: set the Sweave option
1249 to be a character string without dots, and this option will override the
1253 \begin_layout Standard
1254 We have pre-specified an option
1258 for the Sweave package.
1259 This option stops Sweave from loading these two packages:
1262 \begin_layout Standard
1266 \begin_layout Plain Layout
1273 \begin_layout Plain Layout
1277 RequirePackage[T1]{fontenc}
1280 \begin_layout Plain Layout
1287 \begin_layout Plain Layout
1299 \begin_layout Standard
1300 \SpecialCharNoPassThru LyX
1301 can deal with the font encoding by default, so there is no need to ask
1302 Sweave to do this (furthermore, this may bring clashes of package options);
1303 besides, the users can load the
1307 package by themselves in the preamble if needed.
1310 \begin_layout Section
1314 \begin_layout Standard
1315 Since \SpecialCharNoPassThru LyX
1316 2.0, we can see the detailed information during compilation via
1319 View\SpecialChar menuseparator
1323 This is extremely helpful for trouble shooting \twohyphens
1324 the process of R will
1325 be shown in the message panel, and we will be able to know which chunk
1326 goes wrong in cases of errors.
1327 For example, when you compile this document, you can see messages like
1331 \begin_layout Standard
1335 \begin_layout Plain Layout
1342 \begin_layout Plain Layout
1344 17:58:47.868: Processing code chunks with options ...
1347 \begin_layout Plain Layout
1349 17:58:47.885: 1 : echo keep.source term verbatim (label=setup)
1352 \begin_layout Plain Layout
1354 17:58:47.889: 2 : echo keep.source (label=sweave-manual)
1357 \begin_layout Plain Layout
1359 17:58:47.908: 3 : echo keep.source term verbatim (label=print-integers)
1362 \begin_layout Plain Layout
1367 \begin_layout Plain Layout
1369 17:58:47.941: Loading required package: xtable
1372 \begin_layout Plain Layout
1374 17:58:47.976: 9 : echo term verbatim pdf (label=iris-pairs)
1377 \begin_layout Plain Layout
1382 \begin_layout Plain Layout
1384 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1387 \begin_layout Plain Layout
1399 \begin_layout Standard
1405 If any chunk causes an error in R, you will see the error message here
1409 \begin_layout Standard
1410 Another known issue is the Sweave code chunk may fail when we change the
1411 alignment of the chunk paragraph in \SpecialCharNoPassThru LyX
1416 ), because \SpecialCharNoPassThru LyX
1417 may add a macro like
1423 before the code chunk without adding a new line, which will lead to a code
1427 \begin_layout Standard
1431 \begin_layout Plain Layout
1438 \begin_layout Plain Layout
1442 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1445 \begin_layout Plain Layout
1457 \begin_layout Standard
1458 This is not a legitimate Sweave code chunk, since
1462 must start in a new line with nothing before it.
1463 Therefore we recommend you to double check the real source code via
1465 View\SpecialChar menuseparator
1468 when changing the alignment of a chunk paragraph, and make sure
1472 appears in a new line.
1473 This explains why we manually added an empty line in the code chunk in
1475 \begin_inset CommandInset ref
1477 reference "tab:xtable-demo"
1484 \begin_layout Section
1488 \begin_layout Standard
1489 The homepage of Sweave is at
1490 \begin_inset Flex URL
1493 \begin_layout Plain Layout
1495 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1501 The CRAN task view on
1502 \begin_inset Quotes eld
1505 Reproducible Research
1506 \begin_inset Quotes erd
1509 has a list of Sweave-related packages which can be potentially useful;
1511 \begin_inset Flex URL
1514 \begin_layout Plain Layout
1516 http://cran.r-project.org/web/views/ReproducibleResearch.html
1522 Questions about \SpecialCharNoPassThru LyX
1523 and Sweave can be posted to the \SpecialCharNoPassThru LyX
1525 \begin_inset Flex URL
1528 \begin_layout Plain Layout
1530 http://www.lyx.org/MailingLists
1536 More \SpecialCharNoPassThru LyX
1537 examples are provided at
1538 \begin_inset Flex URL
1541 \begin_layout Plain Layout
1543 https://github.com/yihui/lyx
1551 \begin_layout Section
1555 \begin_layout Subsection
1556 How to use Sweave with beamer?
1559 \begin_layout Standard
1560 You will end up with \SpecialCharNoPassThru LaTeX
1561 errors if you use Sweave in normal beamer slides
1562 in \SpecialCharNoPassThru LyX
1563 ; the reason is the beamer frame that contains verbatim R code.
1564 The solution is to use a
1572 , or to pass the option
1576 to the frame title via
1578 Insert\SpecialChar menuseparator
1584 \begin_layout Subsection
1585 Can I define my own R script to compile the Rnw document?
1588 \begin_layout Standard
1590 First you need to understand how the R script
1594 /scripts/lyxsweave.R
1596 works, which was introduced previously, and we recommend you to read the
1597 R source code as well (on how R takes the paths and uses
1601 to handle the Rnw document).
1602 Then you put your customized
1616 Help\SpecialChar menuseparator
1617 About \SpecialCharNoPassThru LyX
1620 Note \SpecialCharNoPassThru LyX
1625 before going to its own
1633 is found in the user directory, it will be used.
1634 It is not recommended to hack the script under the \SpecialCharNoPassThru LyX
1636 directly, since updating \SpecialCharNoPassThru LyX
1637 will override you modified script (the user
1638 directory will not be affected).
1641 \begin_layout Standard
1642 Beside the support for the official Sweave in R, the
1646 package is also supported, which is an alternative tool to Sweave; the
1647 corresponding module is named