Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

::: {.content-hidden unless-format="html"} with plotly seems to end up in pdf output #3254

Closed
4 tasks done
jmbuhr opened this issue Nov 9, 2022 · 6 comments
Closed
4 tasks done
Assignees
Labels
documentation Doc improvements & quarto-web enhancement New feature or request
Milestone

Comments

@jmbuhr
Copy link
Contributor

jmbuhr commented Nov 9, 2022

Bug description

There seems to be an issue with plotly inside of divs that normally shouldn't be run and displayed e.g. in the following qmd file:

---
title: "Untitled"
# prefer-html: true
format:
  html: default
  pdf: default
---

# Test

::: {.content-hidden when-format="html"}
Will not appear in HTML.
:::

This works:

::: {.content-hidden unless-format="html"}
Should appear in the html

```{r}
knitr::kable(mtcars, format = "html")
```
:::


The following fails with

> Error: Functions that produce HTML output found in document targeting pdf output.

::: {.content-hidden unless-format="html"}
Should appear in the html, but not in the pdf:

```{r}
library(tidyverse)
plt <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

plotly::ggplotly(plt)
```

:::

or when forced through `prefer-html: true` with:

> LaTeX Error: Missing \begin{document}.
> 
> See the LaTeX manual or LaTeX Companion for explanation.
> Type  H <return>  for immediate help.
>  ...                                              
>                                                   
> l.78 <
>       script src="test_files/libs/htmlwidgets-1.5.4/htmlwidgets.js"></script> 

quarto version: 1.2.269

Checklist

  • Please include a minimal, fully reproducible example in a single .qmd file? Please provide the whole file rather than the snippet you believe is causing the issue.
  • Please format your issue so it is easier for us to read the bug report.
  • Please document the RStudio IDE version you're running (if applicable), by providing the value displayed in the "About RStudio" main menu dialog?
  • Please document the operating system you're running. If on Linux, please provide the specific distribution.
@jmbuhr jmbuhr added the bug Something isn't working label Nov 9, 2022
@cscheid
Copy link
Collaborator

cscheid commented Nov 9, 2022

The processing of content-hidden happens after the execution. It hides the content: it doesn't prevent it from executing. We should improve our documentation to clarify this situation, but that's currently not something that can be done with content-hidden and content-visible.

In knitr specifically, you can use knitr::is_html_output() to avoid execution.

@cscheid cscheid added documentation Doc improvements & quarto-web enhancement New feature or request and removed bug Something isn't working labels Nov 9, 2022
@cscheid cscheid added this to the Future milestone Nov 9, 2022
@cscheid cscheid self-assigned this Nov 9, 2022
@jmbuhr
Copy link
Contributor Author

jmbuhr commented Nov 9, 2022

That makes sense. I think it was discussed at some point whether it is possible to prevent execution, but with independent engines it seems hard.

However, I still wonder about

Error: Functions that produce HTML output found in document targeting pdf output.

Because if we render to html and look at the intermediat markdown generated by knitr using keep-md: true we find:

::: {.content-hidden unless-format="html"}
(...)
::: {.cell-output-display}
```{=html}
(the plotly output)
```
:::
:::

which renders to pdf just fine with quarto.
(i.e. quarto render test.qmd -t html followed by quarto render test.md -t pdf)

So I think the safeguard in quarto to check for html-producing functions is overeager in this case.

@jmbuhr
Copy link
Contributor Author

jmbuhr commented Nov 9, 2022

Interestingly, if I ignore this error with prefer-html: true things like

<script src="test_files/libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<script src="test_files/libs/plotly-binding-4.10.0/plotly.js"></script>

end up raw in the .tex file, causing it to fail.

So, not using prefer-html and instead quarto render test.qmd -t html followed by quarto render test.md -t pdf works, but quarto render test.qmd -t pdf does not. It's a bit puzzling to me why it works in two steps but not in one. Maybe prefer-html does more than I thought.

@katossky
Copy link

Run into the same issue. Tried to deactivate all cells containing HTML within knitr directly, but HTML content is nevertheless detected and <script> tags injected into md, resulting into the same error as described here.

@cderv
Copy link
Collaborator

cderv commented Apr 4, 2023

@jmbuhr @katossky just to add some explanation context as I am passing by:

  • Using HTML widgets like plotly with knitr computation engine should do a screenshot to insert into PDF output. So no error should be thrown. This requires webshot2 or webshot R 📦 . Detail in Can't compile to PDF when including an R HTML widget #4225 (comment)

  • prefer-html is not to be used with PDF output as it will effectively try to force insertion of HTML. I need to adapt the documentation. This is a R Markdown backported feature that should not be useful for Quarto.

  • @katossky about

    Tried to deactivate all cells containing HTML within knitr directly, but HTML content is nevertheless detected and <script> tags
    injected into md, resulting into the same error as described here.

    I am reading 'within knitr directly' as using R code in a chunk - It may be something else. Can you open an new issue ?

    Thank you

@cderv cderv modified the milestones: Future, v1.4 Nov 23, 2023
@cderv
Copy link
Collaborator

cderv commented Nov 23, 2023

FWIW this has been fixed already and not the OP example works ok.

No leakage anymore in the .tex file of the HTML dependencies.

@cderv cderv closed this as completed Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Doc improvements & quarto-web enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants