如何将图像嵌入到数据框列表中?

How do I embed images in a list of dataframes?

这是一个 post 我几周前 here 提出的相关问题。我正在尝试将二维码嵌入到数据框列表的新列中。 QR 码将包含另一列的数值结果,可以扫描到我们的实验室信息系统中。到目前为止,我能够使用 RMarkdown 生成一个包含包含二维码的列的数据框,但是当我尝试将此代码调整为数据框列表时,我似乎无法做到这一点。

这是一个示例,说明我对单个数据帧的要求:

---
title: "QR Code in Column"
author: "dorton"
date: "2019/01/20"
output: pdf_document
---

```{r mychunk1, fig.show = "hide", echo = FALSE, message=FALSE, warning=FALSE, out.width="72px", include=TRUE}
library(knitr)
library(qrcode)

test <- LETTERS[1:10]
result.one <- round(rnorm(1:10),2)
df <- data.frame(test, result.one, stringsAsFactors = FALSE)
df$result.one <- as.character(df$result.one) #qrcode_gen needs a character input    

invisible(lapply(df$result.one, function(x) qrcode_gen(x)))

out <- cbind(as.character(df$test), 
             as.character(df$result.one), 
             include_graphics(sprintf("![](%s%s-%s.png){width=72px}", 
                                 opts_current$get("fig.path"), 
                                 opts_current$get("label"), 
                                 1:nrow(df))))
kable(out, col.names = c("ID", "Text", "Code"))
```

实际上,我有一个数据帧列表,其中包含 "results" for "test" A-J。我想要的是遍历数据帧列表并在每个数据帧中嵌入二维码,但我似乎无法弄清楚如何做到这一点。这是一个无效的尝试示例:

```{r mychunk, fig.show = "hide", echo = FALSE, message=FALSE, warning=FALSE, out.width="72px"}

library(knitr)
library(qrcode)

test <- LETTERS[1:10]
result.one <- round(rnorm(1:10),2))
result.two <- round(rnorm(11:20), 2)
df1 <- data.frame(test, result.one, stringsAsFactors = FALSE)
df2 <- data.frame(test, result.two, stringsAsFactors = FALSE)
df1$result.one <- as.character(df1$result.one)
df2$result.two <- as.character(df2$result.two)
colnames(df1) <- c("test", "result")
colnames(df2) <- c("test", "result")

df.list <- list(df1, df2)

for (i in length(df.list)){
  invisible(lapply(df.list[[i]]$result, function(x) qrcode_gen(x)))

  out <- cbind(as.character(df.list[[i]]$test), 
             as.character(df.list[[i]]$result), 
             include_graphics(sprintf("![](%s%s-%s.png){width=72px}", 
                                 opts_current$get("fig.path"), 
                                 opts_current$get("label"), 
                                 1:nrow(df.list[[i]]))))
}

  for (i in df.list){
  cat("\n\newpage\n") #This is to add a page break between output tables. I would like to use a pdf output, but I'm not stuck on it.
  kable(out.qr, col.names = c("Test", "Result", "Code"), caption = paste0("Sample ID"))
}

```

我已经尝试了第二个代码块的几个不同的迭代。我希望它与我用 invisible(lapply(df.list[[i]]$result, function(x) qrcode_gen(x))) 生成的 png 的保存位置有关,但我似乎无法找到替代方案。我可以为 png 生成列表列表吗?我一定要吗?

非常感谢任何帮助,我也不会坚持使用 rmarkdown

这有点难理解,灵魂可能不是尽可能优雅。但它有效。如果我明天有时间,我会尝试更深入地了解它。

请注意,二维码的编号从 1 到所有数据框中的总行数。所以我们必须使用计数器 total 来处理这个问题,并在创建它们时保存每个数据帧的代码数量(在 nums 中)。


---
title: "QR Code in Column"
author: "dorton"
date: "2019/01/20"
output: pdf_document     
---


```{r mychunk, echo = FALSE, fig.path =  "qr/", results = 'asis', fig.show='hide'}
library(knitr)
library(qrcode)
df.list <- list(A = data.frame(result = as.character(rnorm(2)),                    
                               test = LETTERS[1:2], stringsAsFactors = F),
                B = data.frame(result = as.character(rnorm(2)),
                               test = LETTERS[3:4], stringsAsFactors = F))

nums <- invisible(sapply(df.list, function(df) {
  lapply(df$result, qrcode_gen)  # create qrcode
  nrow(df)                       # save number of rows of df
}))

path <- paste0(opts_current$get("fig.path"), opts_current$get("label"), "-")

total <- 0
for(i in seq_along(nums)) {
  out <- cbind(df.list[[i]]$test,
               df.list[[i]]$result,
               paste0("![](", path, (1:nums[i]) + total, ".pdf){width=72px}")) 

  print(kable(out))
  total <- total + nums[1] 
}
```