2

Is it possible to use R code, with knitr, for control flow in document generation? For instance, could I write a conditional block in R that output one conclusion under one condition, or another conclusion otherwise; or perhaps use R loops to generate documentation for different input conditions; etc.?

This doesn't work, but to give you an idea of what I'm looking for:

\section{Conclusion}
%% begin.rcode
% if (condition) {
Everything is awesome!
% } else {
All is lost!
% }
% for (x in c(1:3)) {
foo \rinline(x)
% }
%% end.rcode

I would want this to render to (assuming condition is true):

\section{Conclusion}
Everything is awesome!
foo 1
foo 2
foo 3
Xophmeister
  • 8,884
  • 4
  • 44
  • 87
  • you could probably use child documents where the choice of child is based on the condition – Richard Telford May 09 '18 at 16:26
  • I suggest either a chunk option of [`results="asis"`](https://yihui.name/knitr/options/#text-results) or the use of [`knitr::asis_output`](https://stackoverflow.com/a/41135132/3358272). – r2evans May 09 '18 at 17:50
  • 1
    Essentially the same question as https://github.com/yihui/knitr/issues/1539 (and unfortunately the short answer is "no, you cannot do that with knitr") – Yihui Xie May 09 '18 at 22:11

0 Answers0