【问题标题】:How do I embed images in a list of dataframes?如何在数据框列表中嵌入图像?
【发布时间】:2019-02-14 00:45:55
【问题描述】:

这是一个与我几周前here 发布的帖子相关的问题。我正在尝试将 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 的“结果”。我想要的是遍历数据帧列表并在每个数据帧中嵌入二维码,但我似乎无法弄清楚如何做到这一点。下面是一个失败的尝试示例:

```{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 的保存位置有关,但我似乎无法找到替代方案。我可以为 pngs 生成列表列表吗?我一定要吗?

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

【问题讨论】:

    标签: r rstudio r-markdown qr-code


    【解决方案1】:

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

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

    【讨论】:

    • 这是完美的。这么简单……不使用sprintf的原因是什么?
    • 没关系,有什么想法最终是不正确的。 sprintf 也可以使用。
    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 2022-11-28
    • 2022-08-19
    • 2016-11-11
    • 2011-04-07
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多