-
Notifications
You must be signed in to change notification settings - Fork 335
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. |
rmarkdown 2.29 ================================================================================ - `find_external_resources()` now correctly detects knitr child document provided with option like `child = c("child.Rmd")` (thanks, @rempsyc, #2574). - `knit_params_ask()` uses a `select` input for parameters which allow multiple selected values. Previously, a `radio` input was incorrectly used when the parameter had a small number of choices. ```yaml params: primaries: choices: ["red", "yellow", "blue"] multiple: true ``` When `multiple` is not enabled, parameter configuration still uses `radio` when there are fewer than five choices. The `input` parameter field can still be used to force the configuration control. ```yaml params: grade: input: radio choices: ["A", "B", "C", "D", "F"] ``` rmarkdown 2.28 ================================================================================ - Add classes `odd`, `even`, and `header` back to table rows for Pandoc >= 3.2.1, so tables can be styled properly (thanks, @therealgenna, #2567). - `beamer_presentation` support handling latex dependencies via the new `extra_dependencies` argument and declarations within chunks (e.g., `knitr::asis_output("", meta = list(rmarkdown::latex_dependency("longtable")))`) (thanks, @cderv, @atusy, #2478). rmarkdown 2.27 ================================================================================ - Provide a global option `rmarkdown.files.suffix` to configure the suffix of the directory for auxiliary files (thanks, @certara-tzweers, #2550). By default, this suffix is `_files`, which can cause HTML output files to be deleted automatically on Microsoft OneDrive or Google Drive. If that is the case for you, you may set a different suffix in your `.Rprofile`, e.g., `options(rmarkdown.files.suffix = "_rmdfiles")`. - Fix a regression in 2.26 regarding image paths post-processing in `html_document_base()`. Now absolute paths to image in the output directory (`output_dir`) are correctly made relative to the output directory again. rmarkdown 2.26 ================================================================================ - **rmarkdown** now requires **knitr** >= 1.43. - Get rid of the superfluous warning in `find_pandoc()` (thanks, @jszhao, #2527). - Removed the **stringr** dependency since it is used only once in the package and the equivalent base R code is simple enough (thanks, @etiennebacher, #2530). - For the output format option `fig_crop: auto`, it will now use the same logic as in **knitr** to decide if cropping is possible (yihui/knitr#2246). - Avoid corrupting input files by accident (thanks, @J-Moravec, #2534). rmarkdown 2.25 ================================================================================ - Fixed a bug that filenames beginning with `-` cause incorrect invocation of Pandoc (thanks, @mbaynton, #2503). - Documented how to merge `output_format_dependency()` to the output format (thanks, @atusy, #2508). - `ioslides_presentation()` now correctly works with new **shiny** 1.7.5 (thanks, @nicolasgaraycoa, #2514, @gadenbuie, #2516). - Added a new argument `metadata` to the `pre_knit` function in `output_format()` so that users will have access to the YAML metadata of the input document before knitting it (#2485). Please note that if you define `pre_knit` for a custom output format, you are strongly recommended to leave a `...` argument in `pre_knit`, so we (**rmarkdown** package authors) are free to add more arguments to `pre_knit` without breaking your code. If your `pre_knit` function does not have the `...` argument, you will get a warning. rmarkdown 2.24 ================================================================================ - Fixed `file_scope` being lost when extending output formats that considers the `file_scope` using `output_format()`. Merge behavior is to apply overlay `file_scope` function onto the result of `base_format`'s `file_scope` function. This implies that `file_scope` gains second argument which receives the returned values of the base `file_scope` (thanks, @atusy, #2488). - Added `output_format_dependency()` which allows extending output format from within chunks (thanks, @atusy, #2462) - Fix an issue with shiny prerendered document where dependencies context were written twice leasing to parsing error (thanks, @gadenbuie, rstudio/learn#597, #2500). rmarkdown 2.23 ================================================================================ - `find_external_resources()` works with formats defining there own `theme` argument, like `cleanrmd::html_document_clean()`, not related to **bslib** supports (thanks, @gadenbuie, #2493, r-lib/pkgdown#2319). - Fixed version number comparison problems as requested by CRAN. rmarkdown 2.22 ================================================================================ - Using `css` with `.scss` and `.sass` file, or with a bslib theme, now works as expected with a shiny runtime (thanks, @cpsievert, #2443, #2447). - Add a `pandoc_metadata_file_arg()` function to match Pandoc's CLI flag `--metadata-file`. - Mentions that **webshot** or **webshot2** is required to take screenshot of HTML widget. When not installed, an error message mentioning `always_allow_html: true` solution will be shown, but setting this is not the solution (quarto-dev/quarto-cli#4225). - `html_dependency_jqueryui()` updated to 1.13.2 from version bundled in shiny (thanks, @daschnerm, #2477). - Fix an issue with YAML header ending with a commented line containing incomplete yaml (thanks, @keithnewman, #2483). - When code folding is enabled in `html_document()`, the text on the button to show the content has been changed from "Code" to "Show", because the content to show is not necessarily code, e.g., yihui/knitr#2227. rmarkdown 2.21 ================================================================================ - Now HTML output formats use the Font Awesome HTML dependency from the **fontawesome** package instead of shipping an outdated version of Font Awesome in **rmarkdown** (thanks, @rich-iannone, #2451). - Fixed a bug caused by a change in the `is_blank()` function in **xfun** 0.38 (thanks, @andreahgsin, #2469). rmarkdown 2.20 ================================================================================ - The defunct `tufte_handout()` has been removed from **rmarkdown**. Please use `tufte::tufte_handout()` instead. - If an input path to `rmarkdown::render()` is a symbolic link, it is no longer resolved to its real path (thanks, @SamDM @jmw86069, #1508). - Make sure to avoid creating invalid paths when copying resources (thanks, @mnazarov, #2429). - Make sure `logo` is properly embedded in `ioslides_presentation()` when `self_contained = TRUE` (thanks, @mnazarov, #2428).
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: