How do I embed images in a list of dataframes?

本小妞迷上赌 提交于 2019-12-02 10:11:46

This was a little difficult to figure and the soultion might not be as elegant as possible. But it works. If I find some time tomorrow Ill try to get deeper into it.

Notice that qrcodes are numbered from 1 to the total number of rows in all data frames. So we have to take care of this using the counter total and save the number of codes per dataframe when creating them (in 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] 
}
```

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!