| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/utils-sweave.R |
| 3 |
\name{Sweave2knitr} |
| 4 |
\alias{Sweave2knitr} |
| 5 |
\title{Convert Sweave to knitr documents} |
| 6 |
\usage{ |
| 7 |
Sweave2knitr( |
| 8 |
file, |
| 9 |
output = gsub("[.]([^.]+)$", "-knitr.\\\\1", file), |
| 10 |
text = NULL |
| 11 |
) |
| 12 |
} |
| 13 |
\arguments{ |
| 14 |
\item{file}{Path to the Rnw file (must be encoded in UTF-8).} |
| 15 |
|
| 16 |
\item{output}{Output file path. By default, \file{file.Rnw} produces |
| 17 |
\file{file-knitr.Rnw}); if \code{text} is not NULL, no output file will be |
| 18 |
produced.} |
| 19 |
|
| 20 |
\item{text}{An alternative way to provide the Sweave code as a character |
| 21 |
string. If \code{text} is provided, \code{file} will be ignored.} |
| 22 |
} |
| 23 |
\value{ |
| 24 |
If \code{text} is \code{NULL}, the \code{output} file is written and |
| 25 |
\code{NULL} is returned. Otherwise, the converted text string is returned. |
| 26 |
} |
| 27 |
\description{ |
| 28 |
This function converts an Sweave document to a \pkg{knitr}-compatible |
| 29 |
document. |
| 30 |
} |
| 31 |
\details{ |
| 32 |
The pseudo command \samp{\\SweaveInput{file.Rnw}} is converted to a code |
| 33 |
chunk header \code{<<child='file.Rnw'>>=}. |
| 34 |
|
| 35 |
Similarly \samp{\\SweaveOpts{opt = value}} is converted to a code chunk |
| 36 |
\samp{opts_chunk$set(opt = value)} with the chunk option \code{include = |
| 37 |
FALSE}; the options are automatically fixed in the same way as local chunk |
| 38 |
options (explained below). |
| 39 |
|
| 40 |
The Sweave package \samp{\\usepackage{Sweave}} in the preamble is removed |
| 41 |
because it is not required. |
| 42 |
|
| 43 |
Chunk options are updated if necessary: option values \code{true} and |
| 44 |
\code{false} are changed to \code{TRUE} and \code{FALSE} respectively; |
| 45 |
\code{fig=TRUE} is removed because it is not necessary for \pkg{knitr} (plots |
| 46 |
will be automatically generated); \code{fig=FALSE} is changed to |
| 47 |
\code{fig.keep='none'}; the devices \code{pdf/jpeg/png/eps/tikz=TRUE} are |
| 48 |
converted to \code{dev='pdf'/'jpeg'/'png'/'postscript'/'tikz'}; |
| 49 |
\code{pdf/jpeg/png/eps/tikz=FALSE} are removed; |
| 50 |
\code{results=tex/verbatim/hide} are changed to |
| 51 |
\code{results='asis'/'markup'/'hide'}; \code{width/height} are changed to |
| 52 |
\code{fig.width/fig.height}; \code{prefix.string} is changed to |
| 53 |
\code{fig.path}; \code{print/term/prefix=TRUE/FALSE} are removed; most of the |
| 54 |
character options (e.g. \code{engine} and \code{out.width}) are quoted; |
| 55 |
\code{keep.source=TRUE/FALSE} is changed to \code{tidy=FALSE/TRUE} (note the |
| 56 |
order of values). |
| 57 |
|
| 58 |
If a line \code{@} (it closes a chunk) directly follows a previous |
| 59 |
\code{@}, it is removed; if a line \code{@} appears before a code chunk and |
| 60 |
no chunk is before it, it is also removed, because \pkg{knitr} only uses one |
| 61 |
\samp{@} after \samp{<<>>=} by default (which is not the original Noweb |
| 62 |
syntax but more natural). |
| 63 |
} |
| 64 |
\note{ |
| 65 |
If \samp{\\SweaveOpts{}} spans across multiple lines, it will not be |
| 66 |
fixed, and you have to fix it manually. The LaTeX-style syntax of Sweave |
| 67 |
chunks are ignored (see \code{?SweaveSyntaxLatex}); only the Noweb syntax |
| 68 |
is supported. |
| 69 |
} |
| 70 |
\examples{ |
| 71 |
Sweave2knitr(text = "<<echo=TRUE>>=") # this is valid |
| 72 |
Sweave2knitr(text = "<<png=true>>=") # dev='png' |
| 73 |
Sweave2knitr(text = "<<eps=TRUE, pdf=FALSE, results=tex, width=5, prefix.string=foo>>=") |
| 74 |
Sweave2knitr(text = "<<,png=false,fig=TRUE>>=") |
| 75 |
Sweave2knitr(text = "\\\\SweaveOpts{echo=false}") |
| 76 |
Sweave2knitr(text = "\\\\SweaveInput{hello.Rnw}") |
| 77 |
# Sweave example in utils |
| 78 |
testfile = system.file("Sweave", "Sweave-test-1.Rnw", package = "utils") |
| 79 |
Sweave2knitr(testfile, output = "Sweave-test-knitr.Rnw") |
| 80 |
if (interactive()) knit("Sweave-test-knitr.Rnw") # or knit2pdf() directly |
| 81 |
unlink("Sweave-test-knitr.Rnw") |
| 82 |
} |
| 83 |
\references{ |
| 84 |
The motivation of the changes in the syntax: |
| 85 |
\url{https://yihui.org/knitr/demo/sweave/} |
| 86 |
} |
| 87 |
\seealso{ |
| 88 |
\code{\link{Sweave}}, \code{\link{gsub}} |
| 89 |
} |