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

时间:2019-02-14 00:45:55

标签: r rstudio r-markdown qr-code

这与我几周前here发表的帖子有关。我正在尝试将QR码嵌入数据框列表中的新列中。 QR码将包含另一列的数值结果,并且可以扫描到我们的实验室信息系统中。到目前为止,我能够使用RMarkdown生成包含包含QR码的列的数据框,但是当我尝试使此代码适应数据框列表时,似乎无法做到这一点。

这是一个我想用单个数据帧做什么的示例:

---
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"))
```

实际上,我有一个数据框列表,其中包含“测试” A-J的“结果”。我想要的是遍历数据框列表,并在每个数据框中嵌入QR码,但是我似乎无法弄清楚该如何做。这是一个无效的尝试示例:

```{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 个答案:

答案 0 :(得分:3)

这有点难以理解,而且灵魂可能不那么优雅。但这有效。如果明天我有空的话,我会尝试做得更深。

请注意,qr码的编号从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] 
}
```

enter image description here