Rmarkdown 输出中矢量化函数中的替代文本和 ggplot 输出
Alternate text and ggplot output in vectorized function in Rmarkdown output
假设我有一个包含多个变量的数据集,我想通过绘制它们的分布来探索这些变量,例如在每个条形图中。我创建了一个 Rmarkdown 报告,并在一个矢量化函数中自动绘制了这个绘图。足够简单:
---
title: "Doc title"
output: html_notebook
---
```{r setup}
library(tidyverse, magrittr)
opts_knit$set(root.dir = ROOT_DIR)
opts_chunk$set(results = 'asis', warning = FALSE)
```
```{r dataset}
set.seed(303574)
dataset <- tibble(
var_1 = sample.int(4, 20, replace = TRUE),
var_2 = sample.int(7, 20, replace = TRUE)
)
```
# Histograms
```{r plot}
dataset %>% iwalk(
~{
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>% print()
}
)
```
当我编织这份文件时,我得到了预期的结果。现在,我想通过为每个变量添加一个标题来创建一个部分,从而将不同部分中的图分开(在这种情况下,每个变量都有一个选项卡)。
这是我希望渲染我想要的输出:
---
title: "Doc title"
output: html_notebook
---
```{r setup}
library(tidyverse, magrittr)
opts_knit$set(root.dir = ROOT_DIR)
opts_chunk$set(results = 'asis', warning = FALSE)
```
```{r dataset}
set.seed(303574)
dataset <- tibble(
var_1 = sample.int(4, 20, replace = TRUE),
var_2 = sample.int(7, 20, replace = TRUE)
)
```
# Histograms {.tabset}
```{r plot, results='asis'}
dataset %>% iwalk(
~{
paste0("## ", .y) %>% cat(fill = TRUE)
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>% print()
}
)
```
但是,实际情况是两个标题都在 任何情节之前呈现。结果,我每个变量都有一个空选项卡,除了最后一个,所有的图都被一起渲染(在这个最小的例子中,一个空选项卡和另一个包含两个图的选项卡,而不是每个选项卡一个图)。
我如何强制 knitr
交替呈现文本和图表?
我尝试使用 for
循环,即:
for (var in dataset %>% colnames()) {
paste("##", var) %>% cat(fill = TRUE)
qplot(dataset[[var]], xlab = var, geom = "bar") %>% print()
}
但这也没有用。
非常感谢!
P.S.: 请注意这个问题类似于this one;但是,不同之处在于我试图在此处添加自动呈现的标题。
更新(2020-05-06):
事实证明,这仅在输出为笔记本时才会发生(即 html_notebook
);当渲染到 html_document
时,绘图放置正确,不再是问题。
您可以使用此答案中的函数来完成此操作:。问题是 knitr
不会输出正确的 Markdown 源来做你想做的事,所以你需要做它应该做的事。所以定义这个函数:
encodeGraphic <- function(g) {
png(tf1 <- tempfile(fileext = ".png")) # Get an unused filename in the session's temporary directory, and open that file for .png structured output.
print(g) # Output a graphic to the file
dev.off() # Close the file.
txt <- RCurl::base64Encode(readBin(tf1, "raw", file.info(tf1)[1, "size"]), "txt") # Convert the graphic image to a base 64 encoded string.
myImage <- htmltools::HTML(sprintf('<img src="data:image/png;base64,%s">', txt)) # Save the image as a markdown-friendly html object.
return(myImage)
}
然后您可以使用此代码获得您想要的输出。我需要插入一些额外的换行符,但除此之外它基本上就是你所拥有的,运行 到 encodeGraphic
:
```{r plot, results='asis'}
dataset %>% iwalk(
~{
paste0("\n## ", .y, "\n") %>% cat()
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>%
encodeGraphic() %>% cat()
}
)
```
假设我有一个包含多个变量的数据集,我想通过绘制它们的分布来探索这些变量,例如在每个条形图中。我创建了一个 Rmarkdown 报告,并在一个矢量化函数中自动绘制了这个绘图。足够简单:
---
title: "Doc title"
output: html_notebook
---
```{r setup}
library(tidyverse, magrittr)
opts_knit$set(root.dir = ROOT_DIR)
opts_chunk$set(results = 'asis', warning = FALSE)
```
```{r dataset}
set.seed(303574)
dataset <- tibble(
var_1 = sample.int(4, 20, replace = TRUE),
var_2 = sample.int(7, 20, replace = TRUE)
)
```
# Histograms
```{r plot}
dataset %>% iwalk(
~{
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>% print()
}
)
```
当我编织这份文件时,我得到了预期的结果。现在,我想通过为每个变量添加一个标题来创建一个部分,从而将不同部分中的图分开(在这种情况下,每个变量都有一个选项卡)。
这是我希望渲染我想要的输出:
---
title: "Doc title"
output: html_notebook
---
```{r setup}
library(tidyverse, magrittr)
opts_knit$set(root.dir = ROOT_DIR)
opts_chunk$set(results = 'asis', warning = FALSE)
```
```{r dataset}
set.seed(303574)
dataset <- tibble(
var_1 = sample.int(4, 20, replace = TRUE),
var_2 = sample.int(7, 20, replace = TRUE)
)
```
# Histograms {.tabset}
```{r plot, results='asis'}
dataset %>% iwalk(
~{
paste0("## ", .y) %>% cat(fill = TRUE)
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>% print()
}
)
```
但是,实际情况是两个标题都在 任何情节之前呈现。结果,我每个变量都有一个空选项卡,除了最后一个,所有的图都被一起渲染(在这个最小的例子中,一个空选项卡和另一个包含两个图的选项卡,而不是每个选项卡一个图)。
我如何强制 knitr
交替呈现文本和图表?
我尝试使用 for
循环,即:
for (var in dataset %>% colnames()) {
paste("##", var) %>% cat(fill = TRUE)
qplot(dataset[[var]], xlab = var, geom = "bar") %>% print()
}
但这也没有用。
非常感谢!
P.S.: 请注意这个问题类似于this one;但是,不同之处在于我试图在此处添加自动呈现的标题。
更新(2020-05-06):
事实证明,这仅在输出为笔记本时才会发生(即 html_notebook
);当渲染到 html_document
时,绘图放置正确,不再是问题。
您可以使用此答案中的函数来完成此操作:knitr
不会输出正确的 Markdown 源来做你想做的事,所以你需要做它应该做的事。所以定义这个函数:
encodeGraphic <- function(g) {
png(tf1 <- tempfile(fileext = ".png")) # Get an unused filename in the session's temporary directory, and open that file for .png structured output.
print(g) # Output a graphic to the file
dev.off() # Close the file.
txt <- RCurl::base64Encode(readBin(tf1, "raw", file.info(tf1)[1, "size"]), "txt") # Convert the graphic image to a base 64 encoded string.
myImage <- htmltools::HTML(sprintf('<img src="data:image/png;base64,%s">', txt)) # Save the image as a markdown-friendly html object.
return(myImage)
}
然后您可以使用此代码获得您想要的输出。我需要插入一些额外的换行符,但除此之外它基本上就是你所拥有的,运行 到 encodeGraphic
:
```{r plot, results='asis'}
dataset %>% iwalk(
~{
paste0("\n## ", .y, "\n") %>% cat()
dataset %$% qplot(.x, xlab = .y, geom = "bar") %>%
encodeGraphic() %>% cat()
}
)
```