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
80 \paragraph_separation indent
81 \paragraph_indentation default
86 \paperpagestyle default
87 \tracking_changes true
92 \author 2047637253 "Guillaume Munch"
97 \begin_layout Standard
99 \change_inserted 2047637253 1483897614
100 \begin_inset Note Note
103 \begin_layout Plain Layout
105 \change_inserted 2047637253 1483897614
106 \begin_inset Flex Sweave Options
109 \begin_layout Plain Layout
111 \change_inserted 2047637253 1483897614
113 prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE
128 \begin_layout Standard
132 \begin_layout Plain Layout
136 maxwidth was defined in the preamble; see document settings
139 \begin_layout Plain Layout
154 Using Sweave with \SpecialChar LyX
163 \begin_layout Plain Layout
164 Department of Statistics, Iowa State University.
166 \begin_inset CommandInset href
168 name "xie@yihui.name"
169 target "xie@yihui.name"
184 \begin_layout Plain Layout
197 \begin_layout Plain Layout
209 \begin_layout Standard
210 Sweave is a tool in the R language (
211 \begin_inset Flex URL
214 \begin_layout Plain Layout
216 http://www.r-project.org
221 ) for dynamic report generation using the ideas of literate programming.
222 R code can be mixed into a \SpecialChar LaTeX
223 document, and will be executed in R when the
224 document is compiled; the results are written into the \SpecialChar TeX
228 \begin_layout Standard
229 Since \SpecialChar LyX
230 2.0, Sweave can be used out of the box with any document class using
240 module to a \SpecialChar LyX
243 Document\SpecialChar menuseparator
244 Settings\SpecialChar menuseparator
248 In this manual, we demonstrate some common features of Sweave and explain
249 how the module works.
250 A PDF version of this manual can be found at
251 \begin_inset Flex URL
254 \begin_layout Plain Layout
256 https://github.com/downloads/yihui/lyx/sweave.pdf
262 \change_inserted 2047637253 1483898587
266 \begin_layout Standard
268 \change_inserted 2047637253 1483899123
269 Since \SpecialChar LyX
270 2.3, it is necessary to enable the use of
276 Preferences\SpecialChar menuseparator
277 File Handling\SpecialChar menuseparator
280 in order to compile with Sweave.
285 \begin_layout Section
289 \begin_layout Standard
294 module depends on the executable
298 , so it has to be in the
302 variable of the system.
303 It is usually not a problem for Linux and Mac OS users, but may be confusing
304 to Windows users; if you do not understand what is
308 under Windows, you may either use the R script
313 \begin_inset Flex URL
316 \begin_layout Plain Layout
318 https://github.com/yihui/lyx
323 to change it automatically (not recommended; you should learn
329 Tools\SpecialChar menuseparator
330 Preferences\SpecialChar menuseparator
331 Paths\SpecialChar menuseparator
334 to add the bin path of R (recommended), which is often like
346 and you can find it in R:
349 \begin_layout Standard
351 \change_inserted 2047637253 1483897627
352 \begin_inset Flex Chunk
355 \begin_layout Plain Layout
357 \change_inserted 2047637253 1483897627
359 \begin_inset Argument 1
362 \begin_layout Plain Layout
364 \change_inserted 2047637253 1483897627
365 where-is-R-bin,eval=FALSE
373 \begin_layout Plain Layout
375 \change_inserted 2047637253 1483897627
387 \begin_layout Standard
388 After you have done these changes, you need to reconfigure \SpecialChar LyX
391 Tools\SpecialChar menuseparator
407 module will be unavailable.
408 It is recommended to use the latest version of R, since Sweave is still
410 Another dependency is introduced by the \SpecialChar LaTeX
415 which is shipped with R under this path (it is not on CTAN):
418 \begin_layout Standard
420 \change_inserted 2047637253 1483897639
421 \begin_inset Flex Chunk
424 \begin_layout Plain Layout
426 \change_inserted 2047637253 1483897639
428 \begin_inset Argument 1
431 \begin_layout Plain Layout
433 \change_inserted 2047637253 1483897639
434 sweave-sty,eval=FALSE
442 \begin_layout Plain Layout
444 \change_inserted 2047637253 1483897639
446 file.path(R.home('share'), 'texmf', 'tex', 'latex')
456 \begin_layout Standard
458 users may add the texmf directory to the list of root directories in
460 through Mik\SpecialChar TeX
484 , it will directly copy from R, so the Sweave module works even if you do
485 not know where is this style file.
488 \begin_layout Section
492 \begin_layout Standard
493 Before we write an Sweave document, we can set some Sweave options; the
500 in Sweave and it can be inserted in a \SpecialChar LyX
503 Insert\SpecialChar menuseparator
504 Custom Insets\SpecialChar menuseparator
507 , or just use \SpecialChar TeX
510 Insert\SpecialChar menuseparator
515 For example, we can set a global option
519 in the beginning of a document to suppress all R code (by default, both
520 R code and its output will show up in the \SpecialChar LaTeX
525 \begin_layout Standard
526 To write R code in \SpecialChar LyX
528 \change_inserted 2047637253 1483898412
530 \change_deleted 2047637253 1483898419
538 \change_inserted 2047637253 1483898437
541 Insert\SpecialChar menuseparator
542 Custom Insets\SpecialChar menuseparator
544 \change_deleted 2047637253 1483898459
547 environment in the layout styles list (droplist in the top-left corner in
550 , or you can simply open a \SpecialChar TeX
551 code environment and write Sweave code chunks
554 \change_deleted 2047637253 1483898024
555 The latter approach is recommended since it is more stable.
556 \change_inserted 2047637253 1483961753
557 However, \SpecialChar LyX
558 provides a summary of all the
562 insets in the document in the
564 Navigate\SpecialChar menuseparator
569 View\SpecialChar menuseparator
577 \begin_layout Standard
578 Some R options can be useful to the Sweave output as well, e.g.
588 The former option can adjust the width of the output, and the latter can
589 avoid a common problem with quotes.
590 Users are encouraged to read the Sweave manual in R for more details.
593 \begin_layout Standard
595 \change_inserted 2047637253 1483897653
596 \begin_inset Flex Chunk
599 \begin_layout Plain Layout
601 \change_inserted 2047637253 1483897653
603 \begin_inset Argument 1
606 \begin_layout Plain Layout
608 \change_inserted 2047637253 1483897653
609 setup, keep.source=TRUE
617 \begin_layout Plain Layout
619 \change_inserted 2047637253 1483897653
621 ## the option 'digits' can set the (approximate) number of digits
624 \begin_layout Plain Layout
626 \change_inserted 2047637253 1483897653
628 options(width = 70, useFancyQuotes = FALSE, digits = 4)
636 \begin_layout Standard
638 \change_inserted 2047637253 1483897653
639 \begin_inset Flex Chunk
642 \begin_layout Plain Layout
644 \change_inserted 2047637253 1483897653
646 \begin_inset Argument 1
649 \begin_layout Plain Layout
651 \change_inserted 2047637253 1483897653
652 sweave-manual, eval=FALSE, keep.source=TRUE
660 \begin_layout Plain Layout
662 \change_inserted 2047637253 1483897653
664 ## view the Sweave vignette (requires R >= 2.13.0)
667 \begin_layout Plain Layout
669 \change_inserted 2047637253 1483897653
671 vignette("Sweave", package = "utils")
681 \begin_layout Standard
682 A simple example that will run in any S engine:
685 \begin_layout Standard
687 \change_inserted 2047637253 1483897659
688 \begin_inset Flex Chunk
691 \begin_layout Plain Layout
693 \change_inserted 2047637253 1483897659
695 \begin_inset Argument 1
698 \begin_layout Plain Layout
700 \change_inserted 2047637253 1483897659
701 print-integers, keep.source=TRUE
709 \begin_layout Plain Layout
711 \change_inserted 2047637253 1483897659
713 ## the integers from 1 to 10 are
716 \begin_layout Plain Layout
718 \change_inserted 2047637253 1483897659
728 \begin_layout Standard
730 \change_inserted 2047637253 1483897659
731 \begin_inset Flex Chunk
734 \begin_layout Plain Layout
736 \change_inserted 2047637253 1483897659
738 \begin_inset Argument 1
741 \begin_layout Plain Layout
743 \change_inserted 2047637253 1483897659
744 hide-results, results=hide
752 \begin_layout Plain Layout
754 \change_inserted 2047637253 1483897659
756 ## intentionally hide the output by setting results=hide
759 \begin_layout Plain Layout
761 \change_inserted 2047637253 1483897659
773 \begin_layout Standard
774 We can also emulate a simple calculator:
777 \begin_layout Standard
779 \change_inserted 2047637253 1483897665
780 \begin_inset Flex Chunk
783 \begin_layout Plain Layout
785 \change_inserted 2047637253 1483897665
787 \begin_inset Argument 1
790 \begin_layout Plain Layout
792 \change_inserted 2047637253 1483897665
801 \begin_layout Plain Layout
803 \change_inserted 2047637253 1483897665
808 \begin_layout Plain Layout
810 \change_inserted 2047637253 1483897665
822 \begin_layout Standard
823 Now we look at some Gaussian data:
826 \begin_layout Standard
828 \change_inserted 2047637253 1483897670
829 \begin_inset Flex Chunk
832 \begin_layout Plain Layout
834 \change_inserted 2047637253 1483897670
836 \begin_inset Argument 1
839 \begin_layout Plain Layout
841 \change_inserted 2047637253 1483897670
850 \begin_layout Plain Layout
852 \change_inserted 2047637253 1483897670
854 set.seed(123) # for reproducibility
857 \begin_layout Plain Layout
859 \change_inserted 2047637253 1483897670
864 \begin_layout Plain Layout
866 \change_inserted 2047637253 1483897670
871 \begin_layout Plain Layout
873 \change_inserted 2047637253 1483897670
885 \begin_layout Standard
886 Note that we can easily integrate some numbers into standard text; this
887 is done with the command
893 , and the corresponding \SpecialChar LyX
896 Insert\SpecialChar menuseparator
897 Custom Insets\SpecialChar menuseparator
900 , or alternatively just use \SpecialChar TeX
902 For example, the third element of the vector
907 \begin_inset Flex S/R expression
910 \begin_layout Plain Layout
923 \begin_inset Formula $p$
926 -value of the test is
927 \begin_inset Flex S/R expression
930 \begin_layout Plain Layout
932 format.pval(t1$p.value)
938 You can round these numbers using functions like
946 \begin_layout Standard
956 Now we look at a summary of the
977 \begin_layout Standard
979 \change_inserted 2047637253 1483897677
980 \begin_inset Flex Chunk
983 \begin_layout Plain Layout
985 \change_inserted 2047637253 1483897677
987 \begin_inset Argument 1
990 \begin_layout Plain Layout
992 \change_inserted 2047637253 1483897677
1001 \begin_layout Plain Layout
1003 \change_inserted 2047637253 1483897677
1015 \begin_layout Standard
1016 \begin_inset Float table
1021 \begin_layout Plain Layout
1022 \begin_inset Caption Standard
1024 \begin_layout Plain Layout
1025 Quantiles of the first four variables in the
1030 \begin_inset CommandInset label
1032 name "tab:xtable-demo"
1042 \change_inserted 2047637253 1483897685
1044 \begin_inset Flex Chunk
1047 \begin_layout Plain Layout
1049 \change_inserted 2047637253 1483897698
1051 \begin_inset Argument 1
1054 \begin_layout Plain Layout
1056 \change_inserted 2047637253 1483897692
1057 xtable-demo,results=tex,echo=FALSE
1067 \begin_layout Plain Layout
1069 \change_inserted 2047637253 1483897699
1071 if (require('xtable')) {
1074 \begin_layout Plain Layout
1076 \change_inserted 2047637253 1483897699
1078 print(xtable(sapply(iris[,-5], quantile)), floating=FALSE)
1081 \begin_layout Plain Layout
1083 \change_inserted 2047637253 1483897699
1085 } else cat('the xtable package is not available! please install.packages("xtable"
1103 \begin_layout Standard
1104 Usually people do not like such
1105 \begin_inset Quotes eld
1109 \begin_inset Quotes erd
1112 output, and we can polish these messy numbers with the
1118 \begin_inset CommandInset ref
1120 reference "tab:xtable-demo"
1124 is an example demonstrating how to use the
1128 package with Sweave; note the chunk option
1132 makes sure that the R output is written out as raw \SpecialChar LaTeX
1133 code instead of being
1134 wrapped in a special environment (
1141 \begin_layout Standard
1143 \begin_inset CommandInset ref
1145 reference "fig:iris-pairs"
1150 \begin_inset CommandInset ref
1152 reference "fig:iris-boxplots"
1156 are two simple examples of producing graphics output with Sweave.
1159 \begin_layout Standard
1160 \begin_inset Float figure
1165 \begin_layout Plain Layout
1167 \begin_inset Note Note
1170 \begin_layout Plain Layout
1171 Both eps and pdf are set to true so that both latex and pdflatex work.
1172 In practice, you would probably just choose one.
1180 \begin_layout Plain Layout
1183 \change_inserted 2047637253 1483897765
1184 \begin_inset Flex Chunk
1187 \begin_layout Plain Layout
1189 \change_inserted 2047637253 1483897772
1191 \begin_inset Argument 1
1194 \begin_layout Plain Layout
1196 \change_inserted 2047637253 1483897770
1197 iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE
1207 \begin_layout Plain Layout
1209 \change_inserted 2047637253 1483897776
1211 pairs(iris, col = iris$Species)
1223 \begin_layout Plain Layout
1224 \begin_inset Caption Standard
1226 \begin_layout Plain Layout
1227 Scatter plot matrix of the
1232 \begin_inset CommandInset label
1234 name "fig:iris-pairs"
1251 \begin_layout Standard
1252 \begin_inset Float figure
1257 \begin_layout Plain Layout
1260 \change_inserted 2047637253 1483897883
1261 \begin_inset Flex Chunk
1264 \begin_layout Plain Layout
1266 \change_inserted 2047637253 1483897883
1268 \begin_inset Argument 1
1271 \begin_layout Plain Layout
1273 \change_inserted 2047637253 1483897883
1274 iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T
1282 \begin_layout Plain Layout
1284 \change_inserted 2047637253 1483897883
1286 par(mar=c(4,5,.1,.3), las=1)
1289 \begin_layout Plain Layout
1291 \change_inserted 2047637253 1483897883
1293 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1303 \begin_layout Plain Layout
1304 \begin_inset Caption Standard
1306 \begin_layout Plain Layout
1307 Boxplots of sepal length grouped by species.
1308 \begin_inset CommandInset label
1310 name "fig:iris-boxplots"
1327 \begin_layout Standard
1328 We can also export all the R code in an Sweave document as a single R script
1331 File\SpecialChar menuseparator
1332 Export\SpecialChar menuseparator
1335 ), which is done via
1340 \change_inserted 2047637253 1483898877
1344 \begin_layout Section
1352 \begin_layout Standard
1357 module declares that a \SpecialChar LyX
1358 document contains literate programming components
1359 with R, so that \SpecialChar LyX
1360 will handle such documents according to the specific converter
1361 s (see the Customization manual).
1370 converter, which essentially consists of an R script
1378 directory of \SpecialChar LyX
1380 This script takes an
1384 document from \SpecialChar LyX
1385 and call Sweave to process it to a \SpecialChar TeX
1386 document, then \SpecialChar LyX
1388 care of the rest of work.
1391 \begin_layout Standard
1392 The converter from the
1396 format to \SpecialChar LaTeX
1397 is a call to R via the command line as:
1400 \begin_layout Standard
1404 \begin_layout Plain Layout
1411 \begin_layout Plain Layout
1413 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e
1417 \begin_layout Plain Layout
1429 \begin_layout Standard
1430 The \SpecialChar LyX
1435 can be found from the menu
1437 Help\SpecialChar menuseparator
1438 About \SpecialChar LyX
1442 All the rest of options passed to R are explained in the R script, and
1443 the \SpecialChar LyX
1444 Customization manual also has detailed explanations about
1461 General users do not need to know clearly what they mean, but here we need
1462 to explain a few issues in the R code:
1465 \begin_layout Enumerate
1466 the encoding string of the \SpecialChar LyX
1467 document is passed to R as an command line option;
1477 The encoding is used for R to read the
1484 \begin_layout Enumerate
1485 the R code chunks in the Sweave document are executed under the directory
1486 of the \SpecialChar LyX
1487 document (if you are not sure, put
1491 in a code chunk to see what is the current working directory).
1492 In this case, you can put data files under the same directory and use relative
1493 paths in R to guarantee reproducibility, i.e.
1494 we do not need to write hard-coded paths which may not exist in other systems.
1497 \begin_layout Enumerate
1498 if \SpecialChar LaTeX
1503 , this R script will copy it to the temporary directory automatically where
1504 the \SpecialChar LaTeX
1505 compilation takes place.
1508 \begin_layout Enumerate
1509 Sweave will use the filename of the Rnw file as the prefix string for the
1510 graphics output by default; one known issue (
1511 \begin_inset CommandInset href
1514 target "http://www.lyx.org/trac/ticket/7551"
1519 ) is that if the filename (without extension) contains dots, \SpecialChar LaTeX
1521 deal with such files in
1528 Therefore, we have set the default value of the prefix to be the filename
1529 with all the dots being replaced by
1530 \begin_inset Quotes eld
1534 \begin_inset Quotes erd
1538 We can also solve this problem in Sweave directly: set the Sweave option
1543 to be a character string without dots, and this option will override the
1547 \begin_layout Standard
1548 We have pre-specified an option
1552 for the Sweave package.
1553 This option stops Sweave from loading these two packages:
1556 \begin_layout Standard
1560 \begin_layout Plain Layout
1567 \begin_layout Plain Layout
1571 RequirePackage[T1]{fontenc}
1574 \begin_layout Plain Layout
1581 \begin_layout Plain Layout
1593 \begin_layout Standard
1595 can deal with the font encoding by default, so there is no need to ask
1596 Sweave to do this (furthermore, this may bring clashes of package options);
1597 besides, the users can load the
1601 package by themselves in the preamble if needed.
1604 \begin_layout Section
1608 \begin_layout Standard
1609 Since \SpecialChar LyX
1610 2.0, we can see the detailed information during compilation via
1612 View\SpecialChar menuseparator
1616 This is extremely helpful for trouble shooting – the process of R will
1617 be shown in the message panel, and we will be able to know which chunk
1618 goes wrong in cases of errors.
1619 For example, when you compile this document, you can see messages like
1623 \begin_layout Standard
1627 \begin_layout Plain Layout
1634 \begin_layout Plain Layout
1636 17:58:47.868: Processing code chunks with options ...
1639 \begin_layout Plain Layout
1641 17:58:47.885: 1 : echo keep.source term verbatim (label=setup)
1644 \begin_layout Plain Layout
1646 17:58:47.889: 2 : echo keep.source (label=sweave-manual)
1649 \begin_layout Plain Layout
1651 17:58:47.908: 3 : echo keep.source term verbatim (label=print-integers)
1654 \begin_layout Plain Layout
1659 \begin_layout Plain Layout
1661 17:58:47.941: Loading required package: xtable
1664 \begin_layout Plain Layout
1666 17:58:47.976: 9 : echo term verbatim pdf (label=iris-pairs)
1669 \begin_layout Plain Layout
1674 \begin_layout Plain Layout
1676 17:58:48.116: You can now run (pdf)latex on 'sweave.tex'
1679 \begin_layout Plain Layout
1691 \begin_layout Standard
1697 If any chunk causes an error in R, you will see the error message here
1701 \begin_layout Standard
1703 \change_deleted 2047637253 1483899012
1707 \change_inserted 2047637253 1483898966
1708 with the \SpecialChar TeX
1711 is the Sweave code chunk may fail when we change the alignment of the chunk
1712 paragraph in \SpecialChar LyX
1717 ), because \SpecialChar LyX
1718 may add a macro like
1724 before the code chunk without adding a new line, which will lead to a code
1728 \begin_layout Standard
1732 \begin_layout Plain Layout
1739 \begin_layout Plain Layout
1743 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1746 \begin_layout Plain Layout
1758 \begin_layout Standard
1759 This is not a legitimate Sweave code chunk, since
1763 must start in a new line with nothing before it.
1765 \change_inserted 2047637253 1483899083
1766 This does not happen when using the Chunk inset instead of the \SpecialChar TeX
1772 \change_deleted 2047637253 1483898988
1773 Therefore we recommend you to double check the real source code via
1775 View\SpecialChar menuseparator
1778 when changing the alignment of a chunk paragraph, and make sure
1782 appears in a new line.
1783 This explains why we manually added an empty line in the code chunk in
1785 \begin_inset CommandInset ref
1787 reference "tab:xtable-demo"
1796 \begin_layout Section
1800 \begin_layout Standard
1801 The homepage of Sweave is at
1802 \begin_inset Flex URL
1805 \begin_layout Plain Layout
1807 http://www.stat.uni-muenchen.de/~leisch/Sweave/
1813 The CRAN task view on
1814 \begin_inset Quotes eld
1817 Reproducible Research
1818 \begin_inset Quotes erd
1821 has a list of Sweave-related packages which can be potentially useful;
1823 \begin_inset Flex URL
1826 \begin_layout Plain Layout
1828 http://cran.r-project.org/web/views/ReproducibleResearch.html
1834 Questions about \SpecialChar LyX
1835 and Sweave can be posted to the \SpecialChar LyX
1837 \begin_inset Flex URL
1840 \begin_layout Plain Layout
1842 http://www.lyx.org/MailingLists
1848 More \SpecialChar LyX
1849 examples are provided at
1850 \begin_inset Flex URL
1853 \begin_layout Plain Layout
1855 https://github.com/yihui/lyx
1863 \begin_layout Section
1867 \begin_layout Subsection
1868 How to use Sweave with beamer?
1871 \begin_layout Standard
1872 You will end up with \SpecialChar LaTeX
1873 errors if you use Sweave in normal beamer slides in
1875 ; the reason is the beamer frame that contains verbatim R code.
1876 The solution is to use a
1884 , or to pass the option
1888 to the frame title via
1890 Insert\SpecialChar menuseparator
1896 \begin_layout Subsection
1897 Can I define my own R script to compile the Rnw document?
1900 \begin_layout Standard
1902 First you need to understand how the R script
1906 /scripts/lyxsweave.R
1908 works, which was introduced previously, and we recommend you to read the
1909 R source code as well (on how R takes the paths and uses
1913 to handle the Rnw document).
1914 Then you put your customized
1928 Help\SpecialChar menuseparator
1929 About \SpecialChar LyX
1933 Note \SpecialChar LyX
1938 before going to its own
1946 is found in the user directory, it will be used.
1947 It is not recommended to hack the script under the \SpecialChar LyX
1948 library directory directly,
1949 since updating \SpecialChar LyX
1950 will override you modified script (the user directory will
1954 \begin_layout Standard
1955 Beside the support for the official Sweave in R, the
1959 package is also supported, which is an alternative tool to Sweave; the
1960 corresponding module is named