Closed sda030 closed 1 year ago
We can't use your example as it is not self contained. Please provide a complete reproducible example with low or no dependencies.
You can share a Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````
).
````qmd
---
title: "Reproducible Quarto Document"
format: html
---
This is a reproducible Quarto document using `format: html`.
It is written in Markdown and contains embedded R code.
When you run the code, it will produce a plot.
```{r}
plot(cars)
The end.
I guess that what's happening here is that the dependency declared by ggiraph::girafe(ggobj = Svarprosent_uni_cat_prop_plot)
is not format-dependent (it's probably just using knitr to add something to the header, independently of the format).
A good way to confirm this is to try the same thing you're doing but in .pdf format, and see if the latex compilation similarly fails.
Unfortunately, that's not something we can easily fix, because that that block is getting executed before the conditional content evaluation throws it away. I think you should consider using two separate files, one for each format, for the time being.
Or use knitr conditional to avoid ggiraph from injecting its dependencies.
::: {.content-visible when-format="html"}
```{r}
if (knitr::is_html_output()) {
ggiraph::girafe(...)
}
:::
EDIT:
````qmd
---
format:
typst: default
html: default
knitr:
opts_chunk:
dev: png
---
```{r}
#| echo: false
library(ggplot2)
library(ggiraph)
dataset <- mtcars
dataset[["carname"]] <- rownames(mtcars)
gg_base <- ggplot(data = dataset) +
aes(x = wt, y = qsec, colour = disp) +
theme_minimal()
::: {.content-visible when-format="html"}
#| label: fig-interactive
#| fig-cap: "Interactive figure"
if (knitr::is_html_output()) {
ggi <- ggiraph::girafe(
ggobj = gg_base +
aes(tooltip = carname, data_id = carname) +
geom_point_interactive()
)
ggi
}
:::
::: {.content-visible unless-format="html"}
#| label: fig-static
#| fig-cap: "Static figure"
gg_base + geom_point()
:::
Sorry for the poor reprex. Also, I found the real culprit: For other reasons I needed prefer-html: true
. Flipping this to false renders without problems. I guess this connects to what you suggested @mcanouil regarding knitr conditional?
---
format:
typst: default
html: default
prefer-html: true
---
# Test
::: {.content-visible when-format="html"}
```{r}
x <- ggplot2::ggplot(data = mtcars, ggplot2::aes(x=hp, y=mpg)) + ggiraph::geom_point_interactive()
ggiraph::girafe(ggobj = x)
:::
::: {.content-visible unless-format="html"}
x <-
ggplot2::ggplot(data = mtcars, ggplot2::aes(x=hp, y=mpg)) + ggplot2::geom_point()
x
:::
For other reasons I needed prefer-html: true. Flipping this to false renders without problems.
prefer-html
is an option that should be rarely needed. We need to use it internal for some specific format (like Hugo Markdown which supports HTML in markdown). However, most of the time it should not be needed.
When providing HTML only content like HTML widgets, knitr should handle the format requirement by taking a screenshot of the rendered HTML widgets to insert as an image. Using prefer-html: true
will deactivate this.
It seems using prefer-html
with typst does include the dependencies where it shouldn't really. Maybe this is a bug to handle in our knitr related code...
Anyhow, based on that, with prefer-html: false
, using latest Quarto 1.4 and latest knitr with webshot2 or webshot available, this should work without conditional
---
format:
typst:
keep-typ: true
html: default
---
# This is the plot
```{r}
x <- ggplot2::ggplot(data = mtcars, ggplot2::aes(x=hp, y=mpg)) + ggiraph::geom_point_interactive()
x
It will be a screenshot when format is typst
ggiraph::girafe(ggobj = x)
However, you may not want a screenshot for typst as you can insert image ggplot directly without the interactive.
In that case, you indeed need to use conditional. IMO best would be
````markdown
---
format:
typst: default
html: default
---
```{r}
#| include: false
show_graph <- function(x) {
if (knitr::is_html_output()) return(ggiraph::girafe(ggobj = x))
x
}
#| echo: false
x <- ggplot2::ggplot(data = mtcars, ggplot2::aes(x=hp, y=mpg)) + ggiraph::geom_point_interactive()
show_graph(x)
Personal note/feedback:
To note, ggiraph
layout is not the native ggplot2
especially regarding font size, caption location (i.e., ggplot2::labs(caption = ...)
).
I would always prefer to use ggplot2
for static figures rather than "screenshot" of ggiraph
.
I would always prefer to use ggplot2 for static figures rather than "screenshot" of ggiraph.
yes obviously ! Thanks for sharing your feedback.
This was illustrative of the behavior because example in OP example is ggiraph.
To be clear, the clear the behavior is the same for a DT table for example. By default, there will be no static version, so if there is a chunk with a DT table, it will be screenshoted for inclusion as image in Typst output.
Same comment here: It will always be better to a have conditional on format to use a table generation tool that is better looking than a DT screenshot.
Issue is the same anyway, prefer-html
should have no impact on format: typst
. I'll fix that.
From now on, prefer-html
will correctly be ignored in format that don't need it, like typst. This will insure no leak for HTML deps in format that does not support it.
Thanks for the report.
Bug description
I know typst is experimental but maybe something to consider: One might want to set up a website for a report, split into interactive HTML-chapters but also providing a download link to a Typst-generated PDF of the entire thing. Using conditional divs, one can e.g. use interactive graphs (ggiraph) for the HTML-part, and some prettier static graphs for the PDF. So far fine. However, some of the JS dependencies (inherited from the HTML-part) are currently being included in the typ-file, resulting in errors. In my case, I get the errors below (Actual Behavior). If I delete the corresponding 8 lines in the 2022H.typ file and run with
quarto typst compile 2022H.typ
, it compiles beautifully, as expected (Expected Behavior)! Could we avoid placing these dependencies in the typ-file, or at least have an option to exclude them? Due to a more complicated setup with a typst-template, the manual deletion and post-hoc compilation is not a viable workaround for us.Steps to reproduce
First of all apologies for not having an actually reproducible example, and in Norwegian... If needed I can improve it at a later time.
:::
::: {.content-visible unless-format="html"}
:::
Actual behavior
Your environment
Quarto check output