在 rmarkdown HTML 输出中抑制 GIF 的替代文本
Suppress alt text for GIFs in rmarkdown HTML output
我正在使用 RMarkdown 文件中的 gganimate 包生成 GIF。在前面使用 output = github_document
时,GIF 会按预期出现在输出中 (github-document-output). However, when using output = html_document
, the GIF generates with alt text, which defaults to the chunk name (html-document-output)。
有没有办法禁止这种自动字幕?我试过使用 fig.cap
块选项设置我自己的标题,但没有成功。
RMarkdown代码
---
output:
html_document: default
github_document: default
---
```{r}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "output/test-fig-",
cache.path = "output/test-cache-"
)
```
```{r cache = FALSE}
library(knitr)
library(animation)
ani.options(autobrowse = FALSE, interval = 1)
opts_knit$set(animation.fun = function(x, options, format = "gif") {
x = c(knitr:::sans_ext(x), knitr:::file_ext(x))
fig.num = options$fig.num
format = sub("^[.]", "", format)
fig.fname = paste0(sub(paste0(fig.num, "$"), "*", x[1]),
".", x[2])
mov.fname = paste0(sub(paste0(fig.num, "$"), "", x[1]), ".",
format)
# order correctly
figs <- Sys.glob(fig.fname)
figs <- figs[order(as.numeric(stringr::str_match(figs, paste0("(\d+)\.", x[2]))[, 2]))]
animation::im.convert(figs, output = mov.fname)
sprintf("", options$label, paste0(opts_knit$get("base.url"), mov.fname))
})
opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate")
```
```{r pkgs, cache = FALSE}
library(gapminder)
library(ggplot2)
theme_set(theme_bw())
```
```{r setup}
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
geom_point() +
scale_x_log10()
```
```{r dependson = "setup"}
library(gganimate)
gg_animate(p)
```
这里的问题是您使用 markdown 语法包含生成的动画。我猜这引入了一些 iritations。
看看 hook_plot_html
我们可以模拟标准图的默认输出:
sprintf(paste0('<div class="figure %s">',
'<img src="%s">',
'<p class="caption">%s</p>',
'</div>'), options$fig.align, mov.fname, options$fig.cap)
我正在使用 RMarkdown 文件中的 gganimate 包生成 GIF。在前面使用 output = github_document
时,GIF 会按预期出现在输出中 (github-document-output). However, when using output = html_document
, the GIF generates with alt text, which defaults to the chunk name (html-document-output)。
有没有办法禁止这种自动字幕?我试过使用 fig.cap
块选项设置我自己的标题,但没有成功。
RMarkdown代码
---
output:
html_document: default
github_document: default
---
```{r}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "output/test-fig-",
cache.path = "output/test-cache-"
)
```
```{r cache = FALSE}
library(knitr)
library(animation)
ani.options(autobrowse = FALSE, interval = 1)
opts_knit$set(animation.fun = function(x, options, format = "gif") {
x = c(knitr:::sans_ext(x), knitr:::file_ext(x))
fig.num = options$fig.num
format = sub("^[.]", "", format)
fig.fname = paste0(sub(paste0(fig.num, "$"), "*", x[1]),
".", x[2])
mov.fname = paste0(sub(paste0(fig.num, "$"), "", x[1]), ".",
format)
# order correctly
figs <- Sys.glob(fig.fname)
figs <- figs[order(as.numeric(stringr::str_match(figs, paste0("(\d+)\.", x[2]))[, 2]))]
animation::im.convert(figs, output = mov.fname)
sprintf("", options$label, paste0(opts_knit$get("base.url"), mov.fname))
})
opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate")
```
```{r pkgs, cache = FALSE}
library(gapminder)
library(ggplot2)
theme_set(theme_bw())
```
```{r setup}
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
geom_point() +
scale_x_log10()
```
```{r dependson = "setup"}
library(gganimate)
gg_animate(p)
```
这里的问题是您使用 markdown 语法包含生成的动画。我猜这引入了一些 iritations。
看看 hook_plot_html
我们可以模拟标准图的默认输出:
sprintf(paste0('<div class="figure %s">',
'<img src="%s">',
'<p class="caption">%s</p>',
'</div>'), options$fig.align, mov.fname, options$fig.cap)