Current situation: I'm knitting an rMarkdown document, and it's working exactly as expected. When knitting to HTML, each of 10 plotly chunks generate an interactive plotly chart and they work wonderfully. When knitting to PDF or DOCX, each code chunk is shown but each plot does not (as expected), since those file types can't display HTML content.
Goal: Get the plotly plots to render when knitting to HTML, but have ggplots render in their places when knitting to PDF or DOCX (and not echo, eval, or render at all under HTML), without manually adjusting the echo and/or eval parameters for each plot on each knit run.
Tried:
- Can create a param in the YAML header, but couldn't make it adaptable to the output element of the YAML header.
- Couldn't directly access the output element of the YAML header from within a chunk to set an if-else conditional.
Current non-preferred options:
Leave the situation as is, with code showing up in all three outputs but plots only showing up in the HTML output (incomplete report for PDF and DOCX).
Code an additional ggplot2 chunk for each plotly chunk and set all echo and eval to true, resulting in one interactive plot and one static plot for each of 10 variations in the HTML output (20 plots total, very repetitive), as well as the PDF and DOCX outputs echoing and evaluating both code chunks (repetitive) but rendering only the ggplots (large amounts of out-of-place unrendered plotly code).
Code an additional ggplot2 chunk for each plotly chunk, and manually adjust the echo and eval parameters for each knit run (easy to miss something and make a mistake).
Needed: An elegant solution to automatically echo and eval code chunk A along with ignoring code chunk B when knitting HTML, while automatically echoing and evaluating code chunk B along with ignoring code chunk A when knitting PDF or DOCX.