gpt4 book ai didi

r - HTML 中的 Knitr 和图形标题

转载 作者:行者123 更新时间:2023-12-01 23:05:12 26 4
gpt4 key购买 nike

knitr 将参数 fig.cap 定义为

fig.cap: (NULL; character) figure caption to be used in a figure environment in LaTeX (in \caption{}); if NULL or NA, it will be ignored, otherwise a figure environment will be used for the plots in the chunk (output in \begin{figure} and \end{figure})



但是,对于 HTML 输出,以下工作:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---

```{r}
library(ggplot2)
```

```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)

## Plot 2
qplot(carat, depth, data = diamonds)
```

意思是,每个数字都有在代码块参数 fig.cap = c("Caption 1", "Caption 2") 中定义的正确标题

然而,当它们被放置在 block 选项中时,跟踪字幕是具有挑战性的——尤其是在长时间的情况下。除了为每个图形创建两个单独的 block 并在 block 外插入标题之外,还有其他选择吗?

最佳答案

您可以设置eval.after="fig.cap"以便在 block 运行后评估图形标题。这样,您可以在 block 内定义您的标题。

---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---

```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```

```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"

## Plot 2
qplot(carat, depth, data = diamonds)

cap <- c(cap, "This is caption 2")
```

关于r - HTML 中的 Knitr 和图形标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com