1 #LyX 2.4 created this file. For more info see https://www.lyx.org/
5 \save_transient_properties true
6 \origin /systemlyxdir/examples/Modules/
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 no
26 \language_package auto
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
38 \font_typewriter_osf false
39 \font_sf_scale 100 100
40 \font_tt_scale 100 100
42 \use_dash_ligatures false
44 \default_output_format default
46 \bibtex_command default
47 \index_command default
48 \paperfontsize default
53 \use_package amsmath 1
54 \use_package amssymb 1
57 \use_package mathdots 1
58 \use_package mathtools 1
60 \use_package stackrel 1
61 \use_package stmaryrd 1
62 \use_package undertilde 1
64 \cite_engine_type default
68 \paperorientation portrait
84 \paragraph_separation indent
85 \paragraph_indentation default
87 \math_numbering_side default
92 \paperpagestyle default
94 \tracking_changes false
97 \postpone_fragile_content false
100 \html_be_strict false
101 \docbook_table_output 0
102 \docbook_mathml_prefix 1
107 \begin_layout Standard
108 \begin_inset Note Note
111 \begin_layout Plain Layout
112 \begin_inset Flex Sweave Options
115 \begin_layout Plain Layout
117 prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE
130 \begin_layout Standard
134 \begin_layout Plain Layout
138 maxwidth was defined in the preamble;
139 see document settings
142 \begin_layout Plain Layout
157 Using Sweave with \SpecialChar LyX
166 \begin_layout Plain Layout
167 Department of Statistics,
168 Iowa State University.
171 \begin_inset CommandInset href
173 name "xie@yihui.name"
174 target "xie@yihui.name"
189 \begin_layout Plain Layout
202 \begin_layout Plain Layout
214 \begin_layout Standard
215 Sweave is a tool in the R language (
216 \begin_inset Flex URL
219 \begin_layout Plain Layout
221 http://www.r-project.org
226 ) for dynamic report generation using the ideas of literate programming.
227 R code can be mixed into a \SpecialChar LaTeX
229 and will be executed in R when the document is compiled;
230 the results are written into the \SpecialChar TeX
234 \begin_layout Standard
235 Since \SpecialChar LyX
237 Sweave can be used out of the box with any document class using the
246 module to a \SpecialChar LyX
249 Document\SpecialChar menuseparator
250 Settings\SpecialChar menuseparator
255 we demonstrate some common features of Sweave and explain how the module works.
256 A PDF version of this manual can be found at
257 \begin_inset Flex URL
260 \begin_layout Plain Layout
262 https://github.com/downloads/yihui/lyx/sweave.pdf
270 \begin_layout Standard
271 Since \SpecialChar LyX
273 it is necessary to enable the use of
279 Preferences\SpecialChar menuseparator
280 File Handling\SpecialChar menuseparator
283 in order to compile with Sweave.
286 \begin_layout Section
290 \begin_layout Standard
295 module depends on the executable
300 so it has to be in the
304 variable of the system.
305 It is usually not a problem for Linux and Mac OS users,
306 but may be confusing to Windows users;
307 if you do not understand what is
312 you may either use the R script
317 \begin_inset Flex URL
320 \begin_layout Plain Layout
322 https://github.com/yihui/lyx
327 to change it automatically (not recommended;
335 Tools\SpecialChar menuseparator
336 Preferences\SpecialChar menuseparator
337 Paths\SpecialChar menuseparator
340 to add the bin path of R (recommended),
353 and you can find it in R:
356 \begin_layout Standard
357 \begin_inset Flex Chunk
360 \begin_layout Plain Layout
362 \begin_inset Argument 1
365 \begin_layout Plain Layout
367 where-is-R-bin,eval=FALSE
375 \begin_layout Plain Layout
385 \begin_layout Standard
386 After you have done these changes,
387 you need to reconfigure \SpecialChar LyX
390 Tools\SpecialChar menuseparator
407 module will be unavailable.
408 It is recommended to use the latest version of R,
409 since Sweave is still being updated.
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
419 \begin_inset Flex Chunk
422 \begin_layout Plain Layout
424 \begin_inset Argument 1
427 \begin_layout Plain Layout
429 sweave-sty,eval=FALSE
437 \begin_layout Plain Layout
439 file.path(R.home('share'),
450 \begin_layout Standard
452 users may add the texmf directory to the list of root directories in Mik\SpecialChar TeX
453 through Mik\SpecialChar TeX
478 it will directly copy from R,
479 so the Sweave module works even if you do not know where is this style file.
482 \begin_layout Section
486 \begin_layout Standard
487 Before we write an Sweave document,
488 we can set some Sweave options;
495 in Sweave and it can be inserted in a \SpecialChar LyX
498 Insert\SpecialChar menuseparator
499 Custom Insets\SpecialChar menuseparator
503 or just use \SpecialChar TeX
506 Insert\SpecialChar menuseparator
512 we can set a global option
516 in the beginning of a document to suppress all R code (by default,
517 both R code and its output will show up in the \SpecialChar LaTeX
522 \begin_layout Standard
523 To write R code in \SpecialChar LyX
531 Insert\SpecialChar menuseparator
532 Custom Insets\SpecialChar menuseparator
536 or you can simply open a \SpecialChar TeX
537 code environment and write Sweave code chunks in it.
540 provides a summary of all the
544 insets in the document in the
546 Navigate\SpecialChar menuseparator
551 View\SpecialChar menuseparator
557 \begin_layout Standard
558 Some R options can be useful to the Sweave output as well,
569 The former option can adjust the width of the output,
570 and the latter can avoid a common problem with quotes.
571 Users are encouraged to read the Sweave manual in R for more details.
574 \begin_layout Standard
575 \begin_inset Flex Chunk
578 \begin_layout Plain Layout
580 \begin_inset Argument 1
583 \begin_layout Plain Layout
594 \begin_layout Plain Layout
596 ## the option 'digits' can set the (approximate) number of digits
599 \begin_layout Plain Layout
602 useFancyQuotes = FALSE,
611 \begin_layout Standard
612 \begin_inset Flex Chunk
615 \begin_layout Plain Layout
617 \begin_inset Argument 1
620 \begin_layout Plain Layout
632 \begin_layout Plain Layout
634 ## view the Sweave vignette (requires R >= 2.13.0)
637 \begin_layout Plain Layout
648 \begin_layout Standard
649 A simple example that will run in any S engine:
653 \begin_layout Standard
654 \begin_inset Flex Chunk
657 \begin_layout Plain Layout
659 \begin_inset Argument 1
662 \begin_layout Plain Layout
673 \begin_layout Plain Layout
675 ## the integers from 1 to 10 are
678 \begin_layout Plain Layout
688 \begin_layout Standard
689 \begin_inset Flex Chunk
692 \begin_layout Plain Layout
694 \begin_inset Argument 1
697 \begin_layout Plain Layout
708 \begin_layout Plain Layout
710 ## intentionally hide the output by setting results=hide
713 \begin_layout Plain Layout
723 \begin_layout Standard
724 We can also emulate a simple calculator:
728 \begin_layout Standard
729 \begin_inset Flex Chunk
732 \begin_layout Plain Layout
734 \begin_inset Argument 1
737 \begin_layout Plain Layout
747 \begin_layout Plain Layout
752 \begin_layout Plain Layout
762 \begin_layout Standard
763 Now we look at some Gaussian data:
766 \begin_layout Standard
767 \begin_inset Flex Chunk
770 \begin_layout Plain Layout
772 \begin_inset Argument 1
775 \begin_layout Plain Layout
785 \begin_layout Plain Layout
787 set.seed(123) # for reproducibility
790 \begin_layout Plain Layout
795 \begin_layout Plain Layout
800 \begin_layout Plain Layout
810 \begin_layout Standard
811 Note that we can easily integrate some numbers into standard text;
812 this is done with the command
819 and the corresponding \SpecialChar LyX
822 Insert\SpecialChar menuseparator
823 Custom Insets\SpecialChar menuseparator
827 or alternatively just use \SpecialChar TeX
830 the third element of the vector
835 \begin_inset Flex S/R expression
838 \begin_layout Plain Layout
851 \begin_inset Formula $p$
854 -value of the test is
855 \begin_inset Flex S/R expression
858 \begin_layout Plain Layout
860 format.pval(t1$p.value)
866 You can round these numbers using functions like
874 \begin_layout Standard
884 Now we look at a summary of the
905 \begin_layout Standard
906 \begin_inset Flex Chunk
909 \begin_layout Plain Layout
911 \begin_inset Argument 1
914 \begin_layout Plain Layout
924 \begin_layout Plain Layout
934 \begin_layout Standard
935 \begin_inset Float table
942 \begin_layout Plain Layout
943 \begin_inset Caption Standard
945 \begin_layout Plain Layout
946 Quantiles of the first four variables in the
951 \begin_inset CommandInset label
953 name "tab:xtable-demo"
963 \begin_inset Flex Chunk
966 \begin_layout Plain Layout
968 \begin_inset Argument 1
971 \begin_layout Plain Layout
973 xtable-demo,results=tex,echo=FALSE
981 \begin_layout Plain Layout
983 if (require('xtable')) {
986 \begin_layout Plain Layout
988 print(xtable(sapply(iris[,-5],
993 \begin_layout Plain Layout
995 } else cat('the xtable package is not available!
996 please install.packages("xtable")')
1009 \begin_layout Standard
1010 Usually people do not like such
1011 \begin_inset Quotes eld
1015 \begin_inset Quotes erd
1019 and we can polish these messy numbers with the
1025 \begin_inset CommandInset ref
1027 reference "tab:xtable-demo"
1032 is an example demonstrating how to use the
1036 package with Sweave;
1037 note the chunk option
1041 makes sure that the R output is written out as raw \SpecialChar LaTeX
1042 code instead of being wrapped in a special environment (
1049 \begin_layout Standard
1051 \begin_inset CommandInset ref
1053 reference "fig:iris-pairs"
1059 \begin_inset CommandInset ref
1061 reference "fig:iris-boxplots"
1066 are two simple examples of producing graphics output with Sweave.
1069 \begin_layout Standard
1070 \begin_inset Float figure
1077 \begin_layout Plain Layout
1079 \begin_inset Note Note
1082 \begin_layout Plain Layout
1083 Both eps and pdf are set to true so that both latex and pdflatex work.
1085 you would probably just choose one.
1093 \begin_layout Plain Layout
1095 \begin_inset Flex Chunk
1098 \begin_layout Plain Layout
1100 \begin_inset Argument 1
1103 \begin_layout Plain Layout
1105 iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE
1113 \begin_layout Plain Layout
1124 \begin_layout Plain Layout
1125 \begin_inset Caption Standard
1127 \begin_layout Plain Layout
1128 Scatter plot matrix of the
1133 \begin_inset CommandInset label
1135 name "fig:iris-pairs"
1152 \begin_layout Standard
1153 \begin_inset Float figure
1160 \begin_layout Plain Layout
1162 \begin_inset Flex Chunk
1165 \begin_layout Plain Layout
1167 \begin_inset Argument 1
1170 \begin_layout Plain Layout
1172 iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T
1180 \begin_layout Plain Layout
1182 par(mar=c(4,5,.1,.3),
1186 \begin_layout Plain Layout
1188 boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length')
1196 \begin_layout Plain Layout
1197 \begin_inset Caption Standard
1199 \begin_layout Plain Layout
1200 Boxplots of sepal length grouped by species.
1201 \begin_inset CommandInset label
1203 name "fig:iris-boxplots"
1220 \begin_layout Standard
1221 We can also export all the R code in an Sweave document as a single R script (
1223 File\SpecialChar menuseparator
1224 Export\SpecialChar menuseparator
1235 \begin_layout Section
1243 \begin_layout Standard
1248 module declares that a \SpecialChar LyX
1249 document contains literate programming components with R,
1250 so that \SpecialChar LyX
1251 will handle such documents according to the specific converters (see the Customization manual).
1261 which essentially consists of an R script
1269 directory of \SpecialChar LyX
1271 This script takes an
1275 document from \SpecialChar LyX
1276 and call Sweave to process it to a \SpecialChar TeX
1278 then \SpecialChar LyX
1279 takes care of the rest of work.
1282 \begin_layout Standard
1283 The converter from the
1287 format to \SpecialChar LaTeX
1288 is a call to R via the command line as:
1291 \begin_layout Standard
1295 \begin_layout Plain Layout
1302 \begin_layout Plain Layout
1304 Rscript --no-save --no-restore $$s/scripts/lyxsweave.R $$p$$i $$p$$o $$e $$r
1307 \begin_layout Plain Layout
1319 \begin_layout Standard
1320 The \SpecialChar LyX
1325 can be found from the menu
1327 Help\SpecialChar menuseparator
1328 About \SpecialChar LyX
1332 All the rest of options passed to R are explained in the R script,
1333 and the \SpecialChar LyX
1334 Customization manual also has detailed explanations about
1353 General users do not need to know clearly what they mean,
1354 but here we need to explain a few issues in the R code:
1357 \begin_layout Enumerate
1358 the encoding string of the \SpecialChar LyX
1359 document is passed to R as an command line option;
1370 The encoding is used for R to read the
1377 \begin_layout Enumerate
1378 the R code chunks in the Sweave document are executed under the directory of the \SpecialChar LyX
1379 document (if you are not sure,
1384 in a code chunk to see what is the current working directory).
1386 you can put data files under the same directory and use relative paths in R to guarantee reproducibility,
1388 we do not need to write hard-coded paths which may not exist in other systems.
1391 \begin_layout Enumerate
1392 if \SpecialChar LaTeX
1398 this R script will copy it to the temporary directory automatically where the \SpecialChar LaTeX
1399 compilation takes place.
1402 \begin_layout Enumerate
1403 Sweave will use the filename of the Rnw file as the prefix string for the graphics output by default;
1405 \begin_inset CommandInset href
1408 target "https://www.lyx.org/trac/ticket/7551"
1413 ) is that if the filename (without extension) contains dots,
1415 can fail to deal with such files in
1423 we have set the default value of the prefix to be the filename with all the dots being replaced by
1424 \begin_inset Quotes eld
1428 \begin_inset Quotes erd
1432 We can also solve this problem in Sweave directly:
1433 set the Sweave option
1437 to be a character string without dots,
1438 and this option will override the default value.
1441 \begin_layout Standard
1442 We have pre-specified an option
1446 for the Sweave package.
1447 This option stops Sweave from loading these two packages:
1450 \begin_layout Standard
1454 \begin_layout Plain Layout
1461 \begin_layout Plain Layout
1465 RequirePackage[T1]{fontenc}
1468 \begin_layout Plain Layout
1475 \begin_layout Plain Layout
1487 \begin_layout Standard
1489 can deal with the font encoding by default,
1490 so there is no need to ask Sweave to do this (furthermore,
1491 this may bring clashes of package options);
1493 the users can load the
1497 package by themselves in the preamble if needed.
1500 \begin_layout Section
1504 \begin_layout Standard
1505 Since \SpecialChar LyX
1507 we can see the detailed information during compilation via
1509 View\SpecialChar menuseparator
1513 This is extremely helpful for trouble shooting – the process of R will be shown in the message panel,
1514 and we will be able to know which chunk goes wrong in cases of errors.
1516 when you compile this document,
1517 you can see messages like below:
1520 \begin_layout Standard
1524 \begin_layout Plain Layout
1531 \begin_layout Plain Layout
1534 Processing code chunks with options ...
1537 \begin_layout Plain Layout
1541 echo keep.source term verbatim (label=setup)
1544 \begin_layout Plain Layout
1548 echo keep.source (label=sweave-manual)
1551 \begin_layout Plain Layout
1555 echo keep.source term verbatim (label=print-integers)
1558 \begin_layout Plain Layout
1563 \begin_layout Plain Layout
1566 Loading required package:
1570 \begin_layout Plain Layout
1574 echo term verbatim pdf (label=iris-pairs)
1577 \begin_layout Plain Layout
1582 \begin_layout Plain Layout
1585 You can now run (pdf)latex on 'sweave.tex'
1588 \begin_layout Plain Layout
1600 \begin_layout Standard
1606 If any chunk causes an error in R,
1607 you will see the error message here immediately.
1610 \begin_layout Standard
1611 Another issue with the \SpecialChar TeX
1612 code environment is the Sweave code chunk may fail when we change the alignment of the chunk paragraph in \SpecialChar LyX
1618 because \SpecialChar LyX
1619 may add a macro like
1625 before the code chunk without adding a new line,
1626 which will lead to a code chunk like this:
1629 \begin_layout Standard
1633 \begin_layout Plain Layout
1640 \begin_layout Plain Layout
1644 centering{}<<xtable-demo,results=tex,echo=FALSE>>=
1647 \begin_layout Plain Layout
1659 \begin_layout Standard
1660 This is not a legitimate Sweave code chunk,
1665 must start in a new line with nothing before it.
1666 This does not happen when using the Chunk inset instead of the \SpecialChar TeX
1671 \begin_layout Section
1675 \begin_layout Standard
1676 The user manual of Sweave is at
1677 \begin_inset Flex URL
1680 \begin_layout Plain Layout
1682 https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf
1688 The CRAN task view on
1689 \begin_inset Quotes eld
1692 Reproducible Research
1693 \begin_inset Quotes erd
1696 has a list of Sweave-related packages which can be potentially useful;
1698 \begin_inset Flex URL
1701 \begin_layout Plain Layout
1703 http://cran.r-project.org/web/views/ReproducibleResearch.html
1709 Questions about \SpecialChar LyX
1710 and Sweave can be posted to the \SpecialChar LyX
1712 \begin_inset Flex URL
1715 \begin_layout Plain Layout
1717 https://www.lyx.org/MailingLists
1723 More \SpecialChar LyX
1724 examples are provided at
1725 \begin_inset Flex URL
1728 \begin_layout Plain Layout
1730 https://github.com/yihui/lyx
1738 \begin_layout Section
1742 \begin_layout Subsection
1743 How to use Sweave with beamer?
1746 \begin_layout Standard
1747 You will end up with \SpecialChar LaTeX
1748 errors if you use Sweave in normal beamer slides in \SpecialChar LyX
1750 the reason is the beamer frame that contains verbatim R code.
1751 The solution is to use a
1760 or to pass the option
1764 to the frame title via
1766 Insert\SpecialChar menuseparator
1772 \begin_layout Subsection
1773 Can I define my own R script to compile the Rnw document?
1776 \begin_layout Standard
1779 First you need to understand how the R script
1783 /scripts/lyxsweave.R
1786 which was introduced previously,
1787 and we recommend you to read the R source code as well (on how R takes the paths and uses
1791 to handle the Rnw document).
1792 Then you put your customized
1807 Help\SpecialChar menuseparator
1808 About \SpecialChar LyX
1812 Note \SpecialChar LyX
1817 before going to its own
1826 is found in the user directory,
1828 It is not recommended to hack the script under the \SpecialChar LyX
1829 library directory directly,
1830 since updating \SpecialChar LyX
1831 will override you modified script (the user directory will not be affected).
1834 \begin_layout Standard
1835 Beside the support for the official Sweave in R,
1840 package is also supported,
1841 which is an alternative tool to Sweave;
1842 the corresponding module is named