This function converts an Sweave document to a knitr-compatible document.
Sweave2knitr(
file,
output = gsub("[.]([^.]+)$", "-knitr.\\1", file),
text = NULL
)
If text
is NULL
, the output
file is written and
NULL
is returned. Otherwise, the converted text string is returned.
The pseudo command \SweaveInput{file.Rnw} is converted to a code
chunk header <<child='file.Rnw'>>=
.
Similarly \SweaveOpts{opt = value} is converted to a code chunk
opts_chunk$set(opt = value) with the chunk option include =
FALSE
; the options are automatically fixed in the same way as local chunk
options (explained below).
The Sweave package \usepackage{Sweave} in the preamble is removed because it is not required.
Chunk options are updated if necessary: option values true
and
false
are changed to TRUE
and FALSE
respectively;
fig=TRUE
is removed because it is not necessary for knitr (plots
will be automatically generated); fig=FALSE
is changed to
fig.keep='none'
; the devices pdf/jpeg/png/eps/tikz=TRUE
are
converted to dev='pdf'/'jpeg'/'png'/'postscript'/'tikz'
;
pdf/jpeg/png/eps/tikz=FALSE
are removed;
results=tex/verbatim/hide
are changed to
results='asis'/'markup'/'hide'
; width/height
are changed to
fig.width/fig.height
; prefix.string
is changed to
fig.path
; print/term/prefix=TRUE/FALSE
are removed; most of the
character options (e.g. engine
and out.width
) are quoted;
keep.source=TRUE/FALSE
is changed to tidy=FALSE/TRUE
(note the
order of values).
If a line @
(it closes a chunk) directly follows a previous
@
, it is removed; if a line @
appears before a code chunk and
no chunk is before it, it is also removed, because knitr only uses one
@ after <<>>= by default (which is not the original Noweb
syntax but more natural).
If \SweaveOpts{} spans across multiple lines, it will not be
fixed, and you have to fix it manually. The LaTeX-style syntax of Sweave
chunks are ignored (see ?SweaveSyntaxLatex
); only the Noweb syntax
is supported.
The motivation of the changes in the syntax: https://yihui.org/knitr/demo/sweave/
Sweave2knitr(text = "<<echo=TRUE>>=") # this is valid
#> [1] "<<echo=TRUE>>="
Sweave2knitr(text = "<<png=true>>=") # dev='png'
#> capitalizing true/false to TRUE/FALSE:
#> * png=true
#> replacing pdf/jpeg/png/tikz=TRUE with dev='pdf'/'jpeg'/'png'/'tikz':
#> * png=TRUE
#> [1] "<<dev='png'>>="
Sweave2knitr(text = "<<eps=TRUE, pdf=FALSE, results=tex, width=5, prefix.string=foo>>=")
#> removing pdf/jpeg/png/eps/tikz=FALSE:
#> * eps=TRUE, pdf=FALSE, results=tex, width=5, prefix.string=foo
#> replacing eps=TRUE with dev='postscript':
#> * eps=TRUE, , results=tex, width=5, prefix.string=foo
#> replacing results=tex with results=asis:
#> * dev='postscript', , results=tex, width=5, prefix.string=foo
#> quoting the results option:
#> * dev='postscript', , results=asis, width=5, prefix.string=foo
#> replacing width/height with fig.width/fig.height:
#> * dev='postscript', , results='asis', width=5, prefix.string=foo
#> replacing prefix.string=foo with fig.path='foo':
#> * dev='postscript', , results='asis', fig.width=5,
#> prefix.string=foo
#> [1] "<<dev='postscript', results='asis', fig.width=5, fig.path='foo'>>="
Sweave2knitr(text = "<<,png=false,fig=TRUE>>=")
#> capitalizing true/false to TRUE/FALSE:
#> * ,png=false,fig=TRUE
#> removing the unnecessary option fig=TRUE:
#> * ,png=FALSE,fig=TRUE
#> removing pdf/jpeg/png/eps/tikz=FALSE:
#> * ,png=FALSE,
#> [1] "<<>>="
Sweave2knitr(text = "\\SweaveOpts{echo=false}")
#> capitalizing true/false to TRUE/FALSE:
#> * echo=false
#> changing \SweaveOpts{} to opts_chunk$set():
#> * \SweaveOpts{echo=false}
#> [1] "\n<<include=FALSE>>=\nlibrary(knitr)\nopts_chunk$set(\necho=FALSE\n)\n@\n"
Sweave2knitr(text = "\\SweaveInput{hello.Rnw}")
#> replacing \SweaveInput{...} with <<child='...'>>=:
#> * \SweaveInput{hello.Rnw}
#> [1] "\n<<'child-hello.Rnw', child='hello.Rnw'>>=\n@\n"
# Sweave example in utils
testfile = system.file("Sweave", "Sweave-test-1.Rnw", package = "utils")
Sweave2knitr(testfile, output = "Sweave-test-knitr.Rnw")
#> capitalizing true/false to TRUE/FALSE:
#> * fig=true
#> removing the unnecessary option fig=TRUE:
#> * fig=TRUE
#> * fig=TRUE
#> quoting the results option:
#> * results=hide
#> removing options 'print', 'term', 'prefix':
#> * print=TRUE
#> * echo=TRUE,print=TRUE
#> capitalizing true/false to TRUE/FALSE:
#> * echo=true
#> changing \SweaveOpts{} to opts_chunk$set():
#> * \SweaveOpts{echo=FALSE}
#> * \SweaveOpts{echo=true}
#> removing extra lines (#n shows line numbers):
#> * (#69) @
if (interactive()) knit("Sweave-test-knitr.Rnw") # or knit2pdf() directly
unlink("Sweave-test-knitr.Rnw")