Search code examples
rknitrsweave

{Soutput} environment for asis with render_sweave hooks in knitr


I'm processing Rnw files with knitr and want to include the results "asis". In plain knitr this works fine but after enabling the render_sweave hooks an {Soutput} environment is wrapped around the "asis" results. As Sweave() does not have this behavior, this seems to be inconsistent to me. Is this intended/documented behavior? I couldn't find this described anywhere (e.g., https://yihui.name/knitr/demo/sweave/). The reasons this bit me is that I'm not running LaTeX directly on the output but parse it partially first (within the R/exams package).

The issue is illustrated with a minimal foo.Rnw file in knitr style, processed with plain knitr::knit() first:

writeLines('
<<echo=FALSE, results="asis">>=
writeLines("Hello World!")
@
', "foo.Rnw")

knitr::knit("foo.Rnw", quiet = TRUE)
writeLines(readLines("foo.tex"))
##
## Hello World!
##

However, after setting the render_sweave() hook:

knitr::render_sweave()
knitr::knit("foo.Rnw", quiet = TRUE)
writeLines(readLines("foo.tex"))
## 
## \begin{Soutput}
## Hello World!
## \end{Soutput}
## 

In contrast, doing this in Sweave style yields:

writeLines('
<<echo=FALSE, results=tex>>=
writeLines("Hello World!")
@  
', "foo.Rnw")

utils::Sweave("foo.Rnw", quiet = TRUE)
writeLines(readLines("foo.tex"))
## 
## Hello World!

Can I do anything to avoid this behavior, e.g., modify the render_sweave hooks manually?


Solution

  • This is a bug of knitr (many thanks for the report) and I just fixed it on Github. Basically I forgot to consider results='asis' in the output hook. For now, you can try the development version of knitr:

    remotes::install_github('yihui/knitr')