r-markdown - 如何 PDF 呈现具有动态内容的四开本书籍?

标签 r-markdown markdown rstudio quarto

我正在使用 HTML 格式的四开本写论文,其中有一些动态内容(传单 map 、动态图表)。然而,最终,我需要将这本书导出为 PDF/LaTeX,或者至少导出为 Word(然后我可以复制并粘贴到 LaTeX 中)。

当我尝试导出为 PDF 时,我当然遇到了这个错误:

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.

我确实尝试在我的 YAML 文件中添加 always_allow_html: true,但我得到了完全相同的错误。我也试过 conditional rendering使用 {.content-hidden unless-format="pdf"},但我似乎无法让它工作。

有人遇到过同样的问题吗?

最佳答案

使用 .content-visible when-format="html".content-visible when-format="pdf" 工作非常顺利。


---
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
```

:::

关于r-markdown - 如何 PDF 呈现具有动态内容的四开本书籍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73551667/

相关文章:

r - 在chrome中将html保存为pdf

visual-studio-code - VS Code Jupyter Notebook Markdown 显示字体

image - 如何在markdown中显示本 map 片?

r - r : pandas not found 中的 python

python - 用于在 Quarto (`.qmd` )/R Markdown (`Rmd` ) 文件中检查和格式化 Python 代码的工具

r - 忽略编织 R markdown (Rmd) 文件时发生的所有错误?

r - 如何根据输出格式在 Rmarkdown 笔记本中指定主题?

markdown - 在 Markdown 文本中替换占位符(变量)的最简单方法?

r - SparklyR 从 Spark 上下文中删除表

rstudio - bookdown 包 : Why can't I build a book which I could earlier, 收到与 split_by 选项相关的错误消息?