3
votes

Here's what I'd 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 right way to do it?

This question is different from Conditional `echo` (or eval or include) in rmarkdown chunks. That is about how to conditionally evaluate a knitr "chunk". This is about how to conditionally evaluate the "normal" HTML (or TeX, or Markdown) in between two knitr chunks.

1
If you do not insist using knitr, then you might give a try to pander with <% ... %> tags for conditionals and <%= ... %> for printing markdown: rapporter.github.io/pander/#brew-to-pandoc, otherwise you might want to brew::brew your document before passing to knitr. Or maybe there's an internal knitr support for this, I'm not sure :)daroczig
Try putting your HTML (or TeX or md) in a cat statement in a conditionally evaluated chunk.Thomas
@dash2 my above comment is still valid after your editdaroczig
@daroczig yes, brew is one way to go. But I would like to know if there is a knitr solution.user3603486
@Thomas see my comment on arvi1000's answer below.user3603486

1 Answers

1
votes

You can use a normal if block in the chunk. If you want an HTML formatted error message, you can use results='asis' in the chunk options. 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-->