【发布时间】:2020-03-16 19:23:16
【问题描述】:
可以使用 R Markdown 块循环在 HTML 输出中连续(一个在另一个之上)显示多个图形文件(例如 PNG)吗?此循环将识别向量中的文件,例如通过list.files()。
我已经尝试过使用 writeLines("\n")、cat('\r\n\r\n')、knit_expand() [每 this SO 无济于事]。
This R Markdown code(格式如下,但链接为.Rmd)是一个可重现的示例,尝试使用writeLines("\n") 和cat('\r\n\r\n')。请注意,这会将 5 个 R 标志 PNG(仅 12kb)文件副本复制到您的工作目录中。
---
title: "Stack Overflow Consecutive PNG"
author: "Rick Pack"
date: "11/20/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Copy a PNG file as multiple files
```{r png_copy, echo=FALSE}
for (q in 1:5) {
file.copy(list.files(system.file("img", package = "png"),
full.names = TRUE),
getwd())
file.rename("Rlogo.png", paste0("Rlogo_",q,".png"))
}
```
# Only one R logo shown instead of the five available
```{r png_show, echo=FALSE}
library(png)
# Providing the folder so you can delete the png files
# created above
print(getwd())
all_img <- list.files(full.names = TRUE)[grepl(
"Rlogo", list.files())]
for (j in 1:length(all_img)) {
grid::grid.raster(readPNG(all_img[j]))
writeLines("\n")
cat('\r\n\r\n')
cat("\n\n\\pagebreak\n")
}
```
【问题讨论】:
标签: r r-markdown