3

I am writing my thesis using a Quarto book in HTML, which has some dynamic content (leaflet maps, plotly dynamic graphs). However, eventually, I will need to export the book in PDF/LaTeX, or at least Word (and then I can copy and paste into LaTeX).

When I try to export to PDF I of course run into this error:

Functions that produce HTML output found in document targeting pdf output. Please change the output type of this document to HTML. Alternatively, you can allow HTML output in non-HTML formats by adding this option to the YAML front-matter of your rmarkdown file:

always_allow_html: true

Note however that the HTML output will not be visible in non-HTML formats.

I did try to add the always_allow_html: true in my YAML file, but I get the same exact error. I also tried the conditional rendering with {.content-hidden unless-format="pdf"}, but I can't seem to get it working.

Has anyone experienced the same issue?

Roberto
  • 307
  • 2
  • 9
  • Can you try `{.content-hidden unless-format="html"}` instead? – shafee Aug 31 '22 at 07:01
  • I just tried putting ::: {.content-hidden unless-format="html"} {r} R CODE ::: and it does produce a PDF (obviously with no content, but I think I need to put another code to produce a static plot and hide it when the format is HTML. However, if render to HTML I don't see anymore my dynamic content. I just see the code block – Roberto Aug 31 '22 at 16:02
  • Adding a reproducible example with your question that will address the problem would be helpful to suggest a solution to your problem. – shafee Aug 31 '22 at 17:30

3 Answers3

1

Stumbled across this one too. I'm currently checking the output format of pandoc globally

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

and then evaluate chunks conditionally:

(leaflet example from here.)

```{r, echo = F, eval = output != "latex"}
library(leaflet)
leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")  
```

This is optional if you want a note on a missing component in the PDF version:

```{r, echo = F, eval = output == "latex", results = "asis"}
cat("\\textit{Please see the HTML version for interactive content.}")
```

Edit

I just checked, this also works with Quarto documents for me using the below YAML header.

---
title: "Untitled"
format:
  html:
    theme: cosmo
  pdf:
    documentclass: scrreprt
---
Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • I did try this, but: > Error: object "output" not found >Error in yaml::yaml.load(yaml, eval.expr = TRUE) : >Could not evaluate expression: output != "latex" >Calls: .main ... parse_block -> partition_yaml_options -> I did put the output code though, I don't know why it says not found – Roberto Sep 01 '22 at 15:25
  • It works like a charm for me in a plain new quarto book project. Did you copy and these blocks in `index.qmd`? You don't have to modify `_quarto.yml`. – Martin C. Arnold Sep 02 '22 at 07:01
  • I pasted them in my index.qmd before anything else – Roberto Sep 02 '22 at 07:17
  • Did you really do these steps in a new book project (RStudio: New Project > New Directory > Quarto Book)? If so I recommend updating Quarto and RStudio (https://quarto.org/docs/get-started/). It's hard to provide further guidance without any info on software version or a reproducible example. – Martin C. Arnold Sep 02 '22 at 07:38
  • To test this, I did not create a book but a plain new Quarto document. The error is always Error: object "output" not found Error in yaml::yaml.load(yaml, eval.expr = TRUE) : Could not evaluate expression: output == "latex" Calls: .main ... parse_block -> partition_yaml_options -> – Roberto Sep 02 '22 at 07:44
  • I just tried with (non-book) Quarto document and it works too for me (see the YAML header in my edit). As mentioned before: it is hard to help further without knowing package/RStudio versions. Could you provide these? – Martin C. Arnold Sep 02 '22 at 08:06
  • I just copied your header as well, but I get the same error. The R studio version is: RStudio 2022.07.1+554 "Spotted Wakerobin" Release (7872775ebddc40635780ca1ed238934c3345c5de, 2022-07-22) for macOS Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5_1) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36 I am not sure how to check the quarto version though. However, if I add the always_allow_html: true in every page of my book everything works. – Roberto Sep 02 '22 at 09:43
  • 1
    Thank you. I've edited chunk options in my answer. Could please you retry? – Martin C. Arnold Sep 02 '22 at 10:09
  • This does correctly work now! So I need to add the expression inside the {r} opening? – Roberto Sep 02 '22 at 10:33
  • 1
    Yes – it seems that the "Quarto way" to specify chunk options (https://quarto.org/docs/computations/r.html) does not play nicely in every case. My last edit is the "knitr way". :-) – Martin C. Arnold Sep 02 '22 at 11:35
1

I use constructs like below

p <- ggplot()
if (interactive() || opts_knit$get("rmarkdown.pandoc.to") == "html") {
  ggplotly(p)
} else {
  p
}
Thierry
  • 18,049
  • 5
  • 48
  • 66
1

Using .content-visible when-format="html" and .content-visible when-format="pdf" works very smoothly.


---
title: "Conditional Rendering"
format: 
  html: default
  pdf: default
---

## Conditional Content in Quarto

::: {.content-visible when-format="html"}

```{r}
#| message: false

library(plotly)
library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg))
p <-  p + geom_point(aes(colour = factor(cyl)))

ggplotly(p)
```


```{r}
#| message: false
#| fig-pos: "H"
#| fig-width: 4
#| fig-height: 3

library(leaflet)

# took this example from leaflet docs
m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m  # Print the map

```

:::


::: {.content-visible when-format="pdf"}

```{r}

library(plotly)
library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg))
p <-  p + geom_point(aes(colour = factor(cyl)))

p
```

:::

shafee
  • 15,566
  • 3
  • 19
  • 47
  • I am trying the same thing, but if I make a div that is not visible in HTML, the render works and correctly does not show my div. When I do the opposite, which is hiding a leaflet map in PDF and rendering the PDF, it does not work. I will try to create a reproducible example now. Also is there a way to see which exact code line stops the rendering? – Roberto Sep 01 '22 at 15:13
  • Also, just tried your code on a new document and I get the same error, unless I add always_allow_html: true However, adding this to my own code still produces that error – Roberto Sep 01 '22 at 15:30
  • 1
    Ok, the solution is adding to every page: --- always_allow_html: true --- and then using conditional formatting as in your example. – Roberto Sep 01 '22 at 15:48
  • @Roberto, I have tried the code above for quarto book too, it works without using `always_allow_html: true` for both pdf and html as intended. – shafee Sep 01 '22 at 18:32
  • Then it must be something in my RStudio settings? Because I started a completely new quarto document (independent from the quarto book project) and I was getting the same error – Roberto Sep 02 '22 at 05:57