conditionally exclude some bookdown content

I am revising a bookdown html book to include a pdf version for print publication (not for reading the pdf directly). I'd like to get advice on how to conditionally exclude some content based on the output type.

  1. Technical: I have iframes in my Rmd files (not inside code chunks) that embed Youtube videos and gifs in the main column. I also have iframes that display content in the margins when placed inside {marginfigure} code chunks (Tufte-style). Here's an example. Is there a way to suppress these iframes for the pdf output?

  2. Style: I'm looking for examples of books that have an enhanced html version that includes this type of multimedia content AND a standard pdf version (for publishing as a hard copy). I'm interested to see how the print version might reference this omitted material, if at all. For instance, in place of a Youtube video in the print version, is it best practice to include a video icon with a shortlink that the print reader could type into a browser?

  1. You can use knitr::is_latex_output() to conditionally evaluate a chunk see example. See also the recipe " LaTeX or HTML output" in the R Markdown cookbook.

I have no specific suggestion on your second question (the shortlink approach is a nice idea).

2 Likes

Thanks, @maelle. This is great. It seems like this should be the trick.

However, I wonder if I'm misunderstanding how to apply this in my use case. I have some content in margin notes:

```{marginfigure, eval=knitr::is_html_output(), echo=FALSE}
Testing the use of margin notes. Testing the use of margin notes. Testing the use of margin notes. Testing the use of margin notes.
```

And I have iframes not inside any chunk, just in the Rmarkdown body:

My text. More text. See this video.

<iframe>...</iframe>

That was a cool video
  1. Can I use this approach with marginfigure chunks?
  2. Is there a way to use this with content not in chunks? Maybe there is some way to put my iframes in a chunk and then use the approach you showed?

To answer your question 2: You can use inline code in a text chunk

`r ifelse(knitr::is_html_output(),paste('My text. More text. See this video.',knitr::raw_html("<iframe>...</iframe>"),
'That was a cool video'),"")`
2 Likes

@HanOostdijk that's very helpful. Thanks. knitr::raw_html() + @Malle's suggestion to use knitr::is_html_output() works nicely.

1 Like

On the first use case, a marginfigure chunk, I realized I could try tufte::margin_note() instead, so I put this inside a r code chunk:

```{r margin, eval=knitr::is_html_output(), echo=FALSE}
tufte::margin_note("new test")
```

It did not have the intended effect though. The html version renders the following in the main column:

#> [1] "<label for=\"tufte-mn-\" class=\"margin-toggle\">&#8853;</label><input type=\"checkbox\" id=\"tufte-mn-\" class=\"margin-toggle\"><span class=\"marginnote\">new test</span>"

It does not appear in the pdf version (as expected).

This looks like it is producing a string.
Maybe you can use cat to display it (if needed wrapped in html_raw)?

1 Like

OK, we have a winner:

```{r margin, eval=knitr::is_html_output(), echo=FALSE, results='asis'}
cat(tufte::margin_note("new test"))
```

Wrapping in cat() and adding results='asis' to the chunk options.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.