在rmarkdown HTML输出中抑制GIF的alt文本
我正在使用RMarkdown文件中的gganimate包生成GIF。在前面使用在rmarkdown HTML输出中抑制GIF的alt文本,r,knitr,r-markdown,gganimate,R,Knitr,R Markdown,Gganimate,我正在使用RMarkdown文件中的gganimate包生成GIF。在前面使用output=github\u document时,GIF将按预期显示在输出()中。但是,当使用output=html\u document时,GIF会生成alt text,默认为块名() 有没有办法抑制这个自动标题?我尝试使用fig.capchunk选项设置自己的标题,但没有成功 RMarkdown代码 --- output: html_document: default github_document: d
output=github\u document
时,GIF将按预期显示在输出()中。但是,当使用output=html\u document
时,GIF会生成alt text,默认为块名()
有没有办法抑制这个自动标题?我尝试使用fig.cap
chunk选项设置自己的标题,但没有成功
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("![%s](%s)", 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)
```
---
输出:
html_文档:默认值
github_文档:默认值
---
```{r}
knitr::opts_chunk$set(
崩溃=正确,
comment=“#>”,
fig.path=“输出/测试图-”,
cache.path=“输出/测试缓存--”
)
```
```{r cache=FALSE}
图书馆(knitr)
图书馆(动画)
ani.选项(自动浏览=FALSE,间隔=1)
opts_knit$set(animation.fun=function(x,options,format=“gif”){
x=c(knitr:::sans_ext(x),knitr:::file_ext(x))
fig.num=选项$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]),“,
格式)
#正确订购
figs这里的问题是,您将生成的动画包含在标记语法中。我想这会引入一些标记
看一看,我们可以模拟标准图的默认输出:
sprintf(paste0('<div class="figure %s">',
'<img src="%s">',
'<p class="caption">%s</p>',
'</div>'), options$fig.align, mov.fname, options$fig.cap)
sprintf(粘贴0(“”,
'',
“%s
”,
''),选项$fig.align,mov.fname,选项$fig.cap)
在动画中使用sprintf(![%s](%s)”,选项$fig.cap,粘贴0(opts_knit$get(“base.url”),mov.fname))
(然后使用fig.cap
)我认为,在动画中使用fig.cap
。乐趣满足您吗?另外,模拟hook\u plot\html
的默认输出是一个更好的选择:sprintf(“%s
”,选项$fig.align,mov.fname,选项$fig.cap)
@martin Yes,模拟了hook\u plot\u html
的输出,得到了我想要的东西!我在回答时提交,我可以接受它。很高兴它起作用了。