Skip to content

Conversation

@dgkf
Copy link

@dgkf dgkf commented Mar 16, 2019

I ran into a situation where I wanted to be able to turn on or off different pieces of commentary in the markdown throughout a document. For example, I would ideally like to set a flag to include more technical commentary for an analytic audience, but then strip them out for a general audience.

Although the asis engine works in most cases, it doesn't process inline code blocks that I often use for communicating results. To address this, I added a new rmarkdown engine to pre-process the text using knitr::knit.

Here is an example document where this is useful:

---
title: "test"
output: pdf_document
---

```{r, echo=FALSE}
# create a flag to easily turn on/off markdown 'code' blocks
.show_analysis_commentary <- TRUE
```

this is some general text

```{r analysis, echo = FALSE}
# derive answer to life, the universe and everything
var <- 42
```

```{rmarkdown, echo = .show_analysis_commentary}
# print out analysis result only if `.show_analysis_commentary` is `TRUE`
the answer to life, the universe and everything is `r sprintf('%.f', var)`
```

```{r}
hist(mtcars$mpg)
```

let me know if any tests should be added. I couldn't find any for the asis engine and figured this would only necessitate a similar level of testing.

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I don't have time to think about it carefully at the moment. You should probably call knit_child() instead of knit() here. Anyway, I guess the problem should not really block you, because there are at least two ways out:

  1. You can register the engine by yourself via knitr::knit_engines$set(rmarkdown = function(options) ...);

  2. Or use child documents (which will require the commentary to be written to external files, and you may not like that).

@CLAassistant
Copy link

CLAassistant commented Sep 22, 2020

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants