13

When preparing reports using rmarkdown: http://rmarkdown.rstudio.com/ one may want the document to render differently depending upon the document type. For instance I may want to embed a youtube video if the document being rendered is an html file where as if it's pdf or MS Word I would want the hyper-linked URL instead.

Is there a way to tell rmarkdown something like this:

if (html) {
    <iframe width="640" height="390" src="https://www.youtube.com/embed/FnblmZdTbYs?    feature=player_detailpage" frameborder="0" allowfullscreen></iframe>
} else {
    https://www.youtube.com/watch?v=ekBJgsfKnlw
}

code

devtools::install_github("rstudio/rmarkdown")
library(rmarkdown)
render("foo.Rmd", "all")

foo.Rmd

---
title: "For Fun"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    toc: true
    theme: journal
    number_sections: true
  pdf_document:
    toc: true
    number_sections: true
  word_document:
    fig_width: 5
    fig_height: 5
    fig_caption: true
---

## Good times

<iframe width="640" height="390" src="https://www.youtube.com/embed/FnblmZdTbYs?feature=player_detailpage" frameborder="0" allowfullscreen></iframe>
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

3 Answers3

9

As pointed out in an answer to a related question, knitr 1.18 introduced the following functions

knitr::is_html_output()
knitr::is_latex_output()

As the name suggests, is_html_output() checks if the output is HTML. You would add something like this to foo.Rmd:

```{r results='asis'}
if (knitr::is_html_output()) {
    cat('<iframe width="640" height="390" src="https://www.youtube.com/embed/FnblmZdTbYs?        feature=player_detailpage" frameborder="0" allowfullscreen></iframe>')
} else {
    cat("https://www.youtube.com/watch?v=ekBJgsfKnlw")
}
```
Frank
  • 2,386
  • 17
  • 26
6

Yes, you can access the output format via knitr::opts_knit$get("rmarkdown.pandoc.to"). This will return a string with the target output format. Here's an example:

---
title: "Untitled"
output: html_document
---

```{r}
library(knitr)
opts_knit$get("rmarkdown.pandoc.to")
```

This returns "html" for html_document, "docx" for word_document, and "latex" for pdf_document. So to answer your question you can do something like:

html <- knitr::opts_knit$get("rmarkdown.pandoc.to") == "html"
tmpname12345
  • 2,891
  • 18
  • 20
  • 1
    Thanks for answerinf. I should have answered this as I found the answer some time ago: https://trinkerrstuff.wordpress.com/2014/11/18/rmarkdown-alter-action-depending-on-document/ – Tyler Rinker Apr 28 '15 at 19:02
1

Another way, using code chunk options.

Get the output file type at the beginning of the document.

```{r, echo=FALSE}
out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")
```

Then, eval the code chunk depending on the file type:

```{r, results='asis', eval=(out_type=="html"), echo=FALSE}
cat('<iframe width="640" height="390"
        src="https://www.youtube.com/embed/FnblmZdTbYs?feature=player_detailpage"
        frameborder="0" allowfullscreen>
     </iframe>')
```

```{r, results='asis', eval=(out_type!="html"), echo=FALSE}
cat('https://www.youtube.com/embed/FnblmZdTbYs?feature=player_detailpage')
```
Gorka
  • 1,971
  • 1
  • 13
  • 28