Knitr: conditionally evaluate normal chunks

This is what I would like to do in my Rhtml document:

<!--begin.rcode
if (errors==1) {
end.rcode-->
<p>You have an error!</p>
<!--begin.rcode

end.rcode-->

      

Basically I'm trying to use knitr like PHP. The above doesn't work, but is there a correct way to do it?

This question differs from the conditional `echo` (or eval or include) in rmarkdown snippets . It is about how to conditionally evaluate the "piece" of the knife. It's about how to conditionally evaluate "normal" HTML (or TeX, or Markdown) between two smith chunks.

+3


source to share


1 answer


You can use a regular block if

in a chunk. If you require an HTML error message, you can use results='asis'

chunk in the parameters. Like this:



<!--begin.rcode results='asis'
if (errors==1) {
  cat('<p>You have an error</p>')
} else {
  # r code to evaluate when errors!=1
}
end.rcode-->

      

+1


source







All Articles