-
Notifications
You must be signed in to change notification settings - Fork 285
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
Can't compile to PDF when including an R HTML widget #4225
Comments
Using HTML widgets in Rmd or Qmd document with knitr should work the same. We mentions this here: Equivalent of This is what happens for me when I copy paste your example in
Output is a PDF with a screenshot of the leaflet map for me. This is the expected output I believe. This will happen only if one of the webshot tools is available. Can you check ? > knitr:::webshot_available()
webshot2 webshot
TRUE TRUE It is possible you don't have that, and why screenshot does not happen. Installing one of the R package should work. Regarding what happens when ::: {.cell-output-display}
```{=html}
<div class="leaflet html-widget html-fill-item-overflow-hidden html-fill-item" id="htmlwidget-11c313e2f71355dccf95" style="width:1650px;height:1050px;"></div>
<script type="application/json" data-for="htmlwidget-11c313e2f71355dccf95">...</script>
```
::: so it will be ignored by Pandoc when creating the TeX file. However, we are inserting some HTML dependency in the TeX header, which is a bug
I though we had fix this, but it seems not everywhere - Related issue : #1378 I'll look into this @cscheid |
You're right, I didn't have Thanks for the detailed answer ! |
Thanks for confirming. This is indeed an issue I think - in R Markdown, we correctly do not include HTML dependency in non HTML output, even when |
Another related discussion item that shows we need to deal with this better: #5160 as the message is really unclear (coming from rmarkdown and not adapted to Quarto IMO) |
@cderv Is there an equivalent way to get an automatic screenshot with Python? I have a similar snag where I'm using plotly to make graphs for both HTML and PDF output. HTML is fine and the PDF has mime errors. Alternatively, is there a workaround to embed the optionality so that the HTML version renders inline and the PDF gets a screenshot (even if manually serialized and then inserted as an image)? I'm thinking something like hidden code blocks/markdown that only run for the PDF version and some way of setting I didn't find an issue tracking this for Python, but I'm happy to make one or comment there if it helps. |
In R, this is done directly by knitr (used by Quarto) which will detect a htmlwidget content, and trigger the snapshoting to include as an image and not a HTML content. For python, this would require same logic but in jupyter. I know of some ways to to export as an image. For example, with Plotly you can do export to image (https://plotly.com/python/static-image-export#display-bytes-as-image-using-ipythondisplayimage) This would be a way to do it conditionally---
title: "test"
format:
html: default
pdf: default
keep-md: true
---
## Two Figures
```{python}
#| echo: true
#| output: false
import plotly.graph_objects as go
from IPython.display import Image
import numpy as np
np.random.seed(1)
N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y,
mode="markers",
marker=go.scatter.Marker(
size=sz,
color=colors,
opacity=0.6,
colorscale="Viridis"
)
))
```
::: {.content-visible when-format='pdf'}
```{python}
#| echo: false
img_bytes = fig.to_image(format="png")
Image(img_bytes)
```
:::
::: {.content-visible when-format='html'}
```{python}
#| echo: false
fig.show()
```
:::
Not ideal as it will evaluate both cells, but just include the right result. This will be improve when it will be possible to do pre-engine filter. Which is not yet possible. Also, this will be different depending on the tools (with bokeh you would need to export to png using selenium I believe (https://docs.bokeh.org/en/latest/docs/first_steps/first_steps_7.html), but the logic is the same. So I don't think this is auto detected, and automatic, but this can be done with the right conditional. Maybe it can be simplified if Python code in Jupyter does have access to the output format information, but I am not sure it does. (this would allow some Hope it helps |
I'm going to try your sample approach soon with a recent document of mine, but it looks like a huge improvement from where I was. The parts I was missing were (i) using the div syntax, and (ii) using It would still be great to have it be more automatic, but a workaround is a lot better than the edit-and-rerun approach I was using. Thank you! |
To be clear, this is quite specific to IPython, and which tools supports it. it seems bokeh don't for example. The keywords you need to look for is how to output to file in Jupyter. Quarto will run Jupyter for python, so you need the display of cells to output non HTML content for non HTML output.
I agree. I would be really good to have something like that ! |
Thanks again, @cderv! I had a chance to test this out, and it worked well. I did need to split the |
@jtkiley Great ! I will close this issue because the original issue is fixed ---
format: pdf
---
# Leaflet test
```{r}
library(leaflet)
leaflet() %>%
addTiles()
```
now works correctly
So I believe this ok. Please @juba do open a new issue if something is missing or should be clarified. We'll start fresh on the problem. @jtkiley happy to discuss it further in https://github.com/quarto-dev/quarto-cli/discussions or open a new issue with improvement request if you think we should handle it better. I'll add an example about what we discussed though. |
Bug description
Compiling a qmd file to PDF doesn't work if it contains an R HTML widget. Here is a minimal reproducible example with a leaflet map (but the behavior is the same with a visNetwork graph for example) :
When rendering to PDF with
prefer-html: false
, I get the following error message :If I set
prefer-html: true
, I get another error message during xelatex compilation step :Indeed, if I take a look at the generated TeX file there are
<script>
and<link>
tags inserted inside TeX code.I'm using quarto 1.2.335 with Visual Studio code under Linux (KDE Neon / Ubuntu 22.04).
Thanks a lot !
Checklist
The text was updated successfully, but these errors were encountered: