X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fexamples%2Fsweave.lyx;h=e11bcf9fef2771b9ad40ee4f70bd7358c508032e;hb=3fd0cc27d47d79513487e56a254c90e1794b6b0e;hp=90c8a31b2f757814749710b58967dccf37f1ca99;hpb=1602415b98447a2ed7a2d5343c04109078031655;p=lyx.git diff --git a/lib/examples/sweave.lyx b/lib/examples/sweave.lyx index 90c8a31b2f..e11bcf9fef 100644 --- a/lib/examples/sweave.lyx +++ b/lib/examples/sweave.lyx @@ -1,7 +1,8 @@ -#LyX 2.2 created this file. For more info see http://www.lyx.org/ -\lyxformat 501 +#LyX 2.3 created this file. For more info see http://www.lyx.org/ +\lyxformat 544 \begin_document \begin_header +\save_transient_properties true \origin /systemlyxdir/examples/ \textclass article \begin_preamble @@ -35,6 +36,8 @@ sweave \font_osf false \font_sf_scale 100 100 \font_tt_scale 100 100 +\use_microtype false +\use_dash_ligatures false \graphics default \default_output_format default \output_sync 0 @@ -47,23 +50,24 @@ sweave \use_geometry true \use_package amsmath 1 \use_package amssymb 1 -\use_package cancel 0 +\use_package cancel 1 \use_package esint 1 \use_package mathdots 1 -\use_package mathtools 0 +\use_package mathtools 1 \use_package mhchem 1 -\use_package stackrel 0 -\use_package stmaryrd 0 -\use_package undertilde 0 +\use_package stackrel 1 +\use_package stmaryrd 1 +\use_package undertilde 1 \cite_engine basic \cite_engine_type default \biblio_style plain \use_bibtopic false \use_indices false \paperorientation portrait -\suppress_date false +\suppress_date true \justification true \use_refstyle 0 +\use_minted 0 \index Index \shortcut idx \color #008000 @@ -76,7 +80,10 @@ sweave \tocdepth 3 \paragraph_separation indent \paragraph_indentation default -\quotes_language english +\is_math_indent 0 +\math_numbering_side default +\quotes_style english +\dynamic_quotes 0 \papercolumns 1 \papersides 1 \paperpagestyle default @@ -90,14 +97,21 @@ sweave \begin_body \begin_layout Standard -\begin_inset ERT +\begin_inset Note Note status open \begin_layout Plain Layout +\begin_inset Flex Sweave Options +status open + +\begin_layout Plain Layout + +prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE +\end_layout + +\end_inset + -% -\backslash -SweaveOpts{prefix=TRUE,prefix.string=sweave-prefix,keep.source=FALSE} \end_layout \end_inset @@ -131,7 +145,8 @@ maxwidth} \end_layout \begin_layout Title -Using Sweave with \SpecialCharNoPassThru LyX +Using Sweave with \SpecialChar LyX + \end_layout \begin_layout Author @@ -147,6 +162,7 @@ LatexCommand href name "xie@yihui.name" target "xie@yihui.name" type "mailto:" +literal "false" \end_inset @@ -197,16 +213,16 @@ http://www.r-project.org \end_inset ) for dynamic report generation using the ideas of literate programming. - R code can be mixed into a \SpecialCharNoPassThru LaTeX - document, and will be executed in R when - the document is compiled; the results are written into the \SpecialCharNoPassThru TeX + R code can be mixed into a \SpecialChar LaTeX + document, and will be executed in R when the + document is compiled; the results are written into the \SpecialChar TeX output. \end_layout \begin_layout Standard -Since \SpecialCharNoPassThru LyX - 2.0, Sweave can be used out of the box with any document class - using the +Since \SpecialChar LyX + 2.0, Sweave can be used out of the box with any document class using + the \family sans Sweave \family default @@ -215,7 +231,7 @@ Sweave \family sans Sweave \family default - module to a \SpecialCharNoPassThru LyX + module to a \SpecialChar LyX document from \family sans Document\SpecialChar menuseparator @@ -239,6 +255,21 @@ https://github.com/downloads/yihui/lyx/sweave.pdf . \end_layout +\begin_layout Standard +Since \SpecialChar LyX + 2.3, it is necessary to enable the use of +\emph on +needauth +\emph default + converters in +\family sans + Preferences\SpecialChar menuseparator +File Handling\SpecialChar menuseparator +Converters +\family default + in order to compile with Sweave. +\end_layout + \begin_layout Section System Requirements \end_layout @@ -304,22 +335,27 @@ bin \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -R.home('bin') +where-is-R-bin,eval=FALSE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -@ +R.home('bin') \end_layout \end_inset @@ -328,7 +364,7 @@ R.home('bin') \end_layout \begin_layout Standard -After you have done these changes, you need to reconfigure \SpecialCharNoPassThru LyX +After you have done these changes, you need to reconfigure \SpecialChar LyX by \family sans Tools\SpecialChar menuseparator @@ -350,7 +386,7 @@ Sweave module will be unavailable. It is recommended to use the latest version of R, since Sweave is still being updated. - Another dependency is introduced by the \SpecialCharNoPassThru LaTeX + Another dependency is introduced by the \SpecialChar LaTeX style file \family sans Sweave.sty @@ -359,22 +395,27 @@ Sweave.sty \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -file.path(R.home('share'), 'texmf', 'tex', 'latex') +sweave-sty,eval=FALSE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -@ +file.path(R.home('share'), 'texmf', 'tex', 'latex') \end_layout \end_inset @@ -383,10 +424,10 @@ file.path(R.home('share'), 'texmf', 'tex', 'latex') \end_layout \begin_layout Standard -Mik\SpecialCharNoPassThru TeX - users may add the texmf directory to the list of root directories - in Mik\SpecialCharNoPassThru TeX - through Mik\SpecialCharNoPassThru TeX +Mik\SpecialChar TeX + users may add the texmf directory to the list of root directories in + Mik\SpecialChar TeX + through Mik\SpecialChar TeX settings. Further note that \family sans @@ -405,7 +446,7 @@ may ae \series default . - If \SpecialCharNoPassThru LyX + If \SpecialChar LyX cannot find \family sans Sweave.sty @@ -426,18 +467,18 @@ Before we write an Sweave document, we can set some Sweave options; the \backslash SweaveOpts{} \family default - in Sweave and it can be inserted in a \SpecialCharNoPassThru LyX + in Sweave and it can be inserted in a \SpecialChar LyX document by \family sans Insert\SpecialChar menuseparator Custom Insets\SpecialChar menuseparator Sweave Options \family default -, or just use \SpecialCharNoPassThru TeX +, or just use \SpecialChar TeX code to write it ( \family sans Insert\SpecialChar menuseparator -\SpecialCharNoPassThru TeX +\SpecialChar TeX Code \family default ). @@ -446,22 +487,42 @@ Insert\SpecialChar menuseparator echo=FALSE \family default in the beginning of a document to suppress all R code (by default, both - R code and its output will show up in the \SpecialCharNoPassThru LaTeX + R code and its output will show up in the \SpecialChar LaTeX document). \end_layout \begin_layout Standard -To write R code in \SpecialCharNoPassThru LyX -, you can first switch to the +To write R code in \SpecialChar LyX +, you can first insert +\family typewriter +Chunk +\family default + insets from +\family sans +Insert\SpecialChar menuseparator +Custom Insets\SpecialChar menuseparator +Chunk +\family default +, or you can simply open a \SpecialChar TeX + code environment and write Sweave code chunks + in it. + However, \SpecialChar LyX + provides a summary of all the \family typewriter Chunk \family default - environment in the layout styles list (droplist in the top-left corner - in the toolbar), or you can simply open a \SpecialCharNoPassThru TeX - code environment and write - Sweave code chunks in it. - The latter approach is recommended since it is more stable. + insets in the document in the +\family sans +Navigate\SpecialChar menuseparator +Literate Programming +\family default + and +\family sans +View\SpecialChar menuseparator +Outline Pane +\family default + menus. \end_layout \begin_layout Standard @@ -481,12 +542,22 @@ useFancyQuotes \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= +\begin_inset Argument 1 +status open + +\begin_layout Plain Layout + +setup, keep.source=TRUE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout @@ -499,29 +570,38 @@ status open options(width = 70, useFancyQuotes = FALSE, digits = 4) \end_layout -\begin_layout Plain Layout +\end_inset + -@ \end_layout +\begin_layout Standard +\begin_inset Flex Chunk +status open + \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -## view the Sweave vignette (requires R >= 2.13.0) +sweave-manual, eval=FALSE, keep.source=TRUE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -vignette("Sweave", package = "utils") +## view the Sweave vignette (requires R >= 2.13.0) \end_layout \begin_layout Plain Layout -@ +vignette("Sweave", package = "utils") \end_layout \end_inset @@ -534,12 +614,22 @@ A simple example that will run in any S engine: \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= +\begin_inset Argument 1 +status open + +\begin_layout Plain Layout + +print-integers, keep.source=TRUE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout @@ -549,32 +639,41 @@ status open \begin_layout Plain Layout -1:10 +1:10 \end_layout -\begin_layout Plain Layout +\end_inset + -@ \end_layout +\begin_layout Standard +\begin_inset Flex Chunk +status open + \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -## intentionally hide the output by setting results=hide +hide-results, results=hide +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -print(1:20) +## intentionally hide the output by setting results=hide \end_layout \begin_layout Plain Layout -@ +print(1:20) \end_layout \end_inset @@ -587,27 +686,32 @@ We can also emulate a simple calculator: \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -1 + pi +calculator +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -sin(pi/6) +1 + pi \end_layout \begin_layout Plain Layout -@ +sin(pi/6) \end_layout \end_inset @@ -620,12 +724,22 @@ Now we look at some Gaussian data: \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk +status open + +\begin_layout Plain Layout + +\begin_inset Argument 1 status open \begin_layout Plain Layout -<>= +rnorm +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout @@ -648,11 +762,6 @@ print(x) t1 <- t.test(x) \end_layout -\begin_layout Plain Layout - -@ -\end_layout - \end_inset @@ -666,14 +775,14 @@ Note that we can easily integrate some numbers into standard text; this \backslash Sexpr{} \family default -, and the corresponding \SpecialCharNoPassThru LyX +, and the corresponding \SpecialChar LyX menu is \family sans Insert\SpecialChar menuseparator Custom Insets\SpecialChar menuseparator S/R expression \family default -, or alternatively just use \SpecialCharNoPassThru TeX +, or alternatively just use \SpecialChar TeX code. For example, the third element of the vector \family typewriter @@ -751,22 +860,27 @@ iris \end_layout \begin_layout Standard -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -summary(iris[,-5]) +iris-summary +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -@ +summary(iris[,-5]) \end_layout \end_inset @@ -801,20 +915,22 @@ name "tab:xtable-demo" \end_inset -\end_layout +\begin_inset Flex Chunk +status open \begin_layout Plain Layout -\align center -\begin_inset ERT + +\begin_inset Argument 1 status open \begin_layout Plain Layout +xtable-demo,results=tex,echo=FALSE \end_layout -\begin_layout Plain Layout +\end_inset + -<>= \end_layout \begin_layout Plain Layout @@ -833,11 +949,6 @@ print(xtable(sapply(iris[,-5], quantile)), floating=FALSE) )') \end_layout -\begin_layout Plain Layout - -@ -\end_layout - \end_inset @@ -877,9 +988,9 @@ xtable \family typewriter results=tex \family default - makes sure that the R output is written out as raw \SpecialCharNoPassThru LaTeX - code instead of - being wrapped in a special environment ( + makes sure that the R output is written out as raw \SpecialChar LaTeX + code instead of being + wrapped in a special environment ( \family typewriter Soutput \family default @@ -923,22 +1034,31 @@ Both eps and pdf are set to true so that both latex and pdflatex work. \end_inset -\begin_inset ERT +\end_layout + +\begin_layout Plain Layout +\align center +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -pairs(iris, col = iris$Species) +iris-pairs,fig=TRUE,width=4.5,height=4.5,eps=TRUE,pdf=TRUE +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -@ +pairs(iris, col = iris$Species) \end_layout \end_inset @@ -982,27 +1102,32 @@ status open \begin_layout Plain Layout \align center -\begin_inset ERT +\begin_inset Flex Chunk status open \begin_layout Plain Layout -<>= -\end_layout +\begin_inset Argument 1 +status open \begin_layout Plain Layout -par(mar=c(4,5,.1,.3), las=1) +iris-boxplots,fig=TRUE,width=7,height=2,keep.source=F,eps=T,pdf=T +\end_layout + +\end_inset + + \end_layout \begin_layout Plain Layout -boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length') +par(mar=c(4,5,.1,.3), las=1) \end_layout \begin_layout Plain Layout -@ +boxplot(Sepal.Length~Species,data=iris,horizontal=TRUE,xlab='Sepal.Length') \end_layout \end_inset @@ -1062,11 +1187,11 @@ The \family sans Sweave \family default - module declares that a \SpecialCharNoPassThru LyX + module declares that a \SpecialChar LyX document contains literate programming components - with R, so that \SpecialCharNoPassThru LyX - will handle such documents according to the specific - converters (see the Customization manual). + with R, so that \SpecialChar LyX + will handle such documents according to the specific converter +s (see the Customization manual). The \family sans Sweave @@ -1083,17 +1208,17 @@ lyxsweave.R \family sans scripts \family default - directory of \SpecialCharNoPassThru LyX + directory of \SpecialChar LyX . This script takes an \family sans Rnw \family default - document from \SpecialCharNoPassThru LyX - and call Sweave to process it to a \SpecialCharNoPassThru TeX - document, then - \SpecialCharNoPassThru LyX - takes care of the rest of work. + document from \SpecialChar LyX + and call Sweave to process it to a \SpecialChar TeX + document, then \SpecialChar LyX + takes + care of the rest of work. \end_layout \begin_layout Standard @@ -1101,7 +1226,7 @@ The converter from the \family sans sweave \family default - format to \SpecialCharNoPassThru LaTeX + format to \SpecialChar LaTeX is a call to R via the command line as: \end_layout @@ -1135,7 +1260,7 @@ end{verbatim} \end_layout \begin_layout Standard -The \SpecialCharNoPassThru LyX +The \SpecialChar LyX library directory \family typewriter $$s @@ -1143,11 +1268,12 @@ $$s can be found from the menu \family sans Help\SpecialChar menuseparator -About \SpecialCharNoPassThru LyX +About \SpecialChar LyX + \family default . All the rest of options passed to R are explained in the R script, and - the \SpecialCharNoPassThru LyX + the \SpecialChar LyX Customization manual also has detailed explanations about \family typewriter $$i @@ -1170,9 +1296,9 @@ $$r \end_layout \begin_layout Enumerate -the encoding string of the \SpecialCharNoPassThru LyX - document is passed to R as an command line - option; possible values are +the encoding string of the \SpecialChar LyX + document is passed to R as an command line option; + possible values are \family typewriter ISO-8859-15 \family default @@ -1190,7 +1316,7 @@ Rnw \begin_layout Enumerate the R code chunks in the Sweave document are executed under the directory - of the \SpecialCharNoPassThru LyX + of the \SpecialChar LyX document (if you are not sure, put \family typewriter getwd() @@ -1202,13 +1328,13 @@ getwd() \end_layout \begin_layout Enumerate -if \SpecialCharNoPassThru LaTeX +if \SpecialChar LaTeX cannot find \family sans Sweave.sty \family default , this R script will copy it to the temporary directory automatically where - the \SpecialCharNoPassThru LaTeX + the \SpecialChar LaTeX compilation takes place. \end_layout @@ -1218,13 +1344,14 @@ Sweave will use the filename of the Rnw file as the prefix string for the \begin_inset CommandInset href LatexCommand href name "#7551" -target "http://www.lyx.org/trac/ticket/7551" +target "https://www.lyx.org/trac/ticket/7551" +literal "false" \end_inset -) is that if the filename (without extension) contains dots, \SpecialCharNoPassThru LaTeX - can fail - to deal with such files in +) is that if the filename (without extension) contains dots, \SpecialChar LaTeX + can fail to + deal with such files in \family typewriter \backslash @@ -1297,7 +1424,7 @@ end{verbatim} \end_layout \begin_layout Standard -\SpecialCharNoPassThru LyX +\SpecialChar LyX can deal with the font encoding by default, so there is no need to ask Sweave to do this (furthermore, this may bring clashes of package options); besides, the users can load the @@ -1312,16 +1439,14 @@ Trouble Shooting \end_layout \begin_layout Standard -Since \SpecialCharNoPassThru LyX - 2.0, we can see the detailed information during compilation via - +Since \SpecialChar LyX + 2.0, we can see the detailed information during compilation via \family sans View\SpecialChar menuseparator View Messages \family default . - This is extremely helpful for trouble shooting \twohyphens - the process of R will + This is extremely helpful for trouble shooting – the process of R will be shown in the message panel, and we will be able to know which chunk goes wrong in cases of errors. For example, when you compile this document, you can see messages like @@ -1407,13 +1532,14 @@ label= \end_layout \begin_layout Standard -Another known issue is the Sweave code chunk may fail when we change the - alignment of the chunk paragraph in \SpecialCharNoPassThru LyX +Another issue with the \SpecialChar TeX + code environment is the Sweave code chunk may fail + when we change the alignment of the chunk paragraph in \SpecialChar LyX (using \family sans Paragraph settings \family default -), because \SpecialCharNoPassThru LyX +), because \SpecialChar LyX may add a macro like \family typewriter @@ -1460,25 +1586,10 @@ This is not a legitimate Sweave code chunk, since <<>>= \family default must start in a new line with nothing before it. - Therefore we recommend you to double check the real source code via -\family sans -View\SpecialChar menuseparator -View Source -\family default - when changing the alignment of a chunk paragraph, and make sure -\family typewriter -<<>>= -\family default - appears in a new line. - This explains why we manually added an empty line in the code chunk in - Table -\begin_inset CommandInset ref -LatexCommand ref -reference "tab:xtable-demo" - -\end_inset - -. + This does not happen when using the Chunk inset instead of the \SpecialChar TeX + code environmen +t. + \end_layout \begin_layout Section @@ -1486,13 +1597,13 @@ Resources \end_layout \begin_layout Standard -The homepage of Sweave is at +The user manual of Sweave is at \begin_inset Flex URL status collapsed \begin_layout Plain Layout -http://www.stat.uni-muenchen.de/~leisch/Sweave/ +https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf \end_layout \end_inset @@ -1519,21 +1630,21 @@ http://cran.r-project.org/web/views/ReproducibleResearch.html \end_inset . - Questions about \SpecialCharNoPassThru LyX - and Sweave can be posted to the \SpecialCharNoPassThru LyX + Questions about \SpecialChar LyX + and Sweave can be posted to the \SpecialChar LyX mailing list ( \begin_inset Flex URL status collapsed \begin_layout Plain Layout -http://www.lyx.org/MailingLists +https://www.lyx.org/MailingLists \end_layout \end_inset ). - More \SpecialCharNoPassThru LyX + More \SpecialChar LyX examples are provided at \begin_inset Flex URL status collapsed @@ -1557,9 +1668,9 @@ How to use Sweave with beamer? \end_layout \begin_layout Standard -You will end up with \SpecialCharNoPassThru LaTeX - errors if you use Sweave in normal beamer slides - in \SpecialCharNoPassThru LyX +You will end up with \SpecialChar LaTeX + errors if you use Sweave in normal beamer slides in + \SpecialChar LyX ; the reason is the beamer frame that contains verbatim R code. The solution is to use a \emph on @@ -1614,10 +1725,11 @@ user directory (again, see \family sans Help\SpecialChar menuseparator -About \SpecialCharNoPassThru LyX +About \SpecialChar LyX + \family default ). - Note \SpecialCharNoPassThru LyX + Note \SpecialChar LyX will check the \emph on user directory @@ -1631,11 +1743,11 @@ library directory lyxsweave.R \family default is found in the user directory, it will be used. - It is not recommended to hack the script under the \SpecialCharNoPassThru LyX - library directory - directly, since updating \SpecialCharNoPassThru LyX - will override you modified script (the user - directory will not be affected). + It is not recommended to hack the script under the \SpecialChar LyX + library directory directly, + since updating \SpecialChar LyX + will override you modified script (the user directory will + not be affected). \end_layout \begin_layout Standard