简体   繁体   中英

How can I generate a QR Code containing data from a dataframe in R?

I have a laboratory analyzer that generates results in a .csv or .xlsx format, however right now I must manually type the results from the output into our result tracking software system manually because copy-paste doesn't work.

I would like to write an R script that can translate the results from this .csv into the software program, and the best way I can think of is to generate the results as a barcode/QR code which I can then scan in to the software program. To do this, I need a tab-delimited output to be contained within the QR code. So far, I can generate a QR code using the package qrcode result-by-result, but if I have 50+ results, I can't generate a QR code for each, and I can't figure out how to get the qrcode package to give me what I need.

# Example dataframe
test <- LETTERS[1:10]
result.one <- rnorm(1:10)
result.two <- rnorm(1:10)
df <- data.frame(test, result.one, result.two)

Expected output is a QR code that can be scanned to generate results that look something like the output from this code:

library("openxlsx")
library("dplyr")
write.xlsx(select(df, test, result.one), file = "H:/R/junk1.xlsx")
write.xlsx(select(df, test, result.two), file = "H:/R/junk2.xlsx")

where junk1 would be one QR code, junk2 would be another, etc...

If I can figure this out, I can save my staff hours of tedious work every day... so this would be a great help!

I'm posting this as an answer to my own question, although I'm not going to accept it because I'm not completely doing what I want to. I would still prefer not to have to scan 16 qr codes for every result column, but this is all I can come up with right now.

I am generating a QR code for each result in the "result.one" column as a jpeg, then pasting the jpeg into a new column. In the end, I'm going to parse out each "result.X" column and paste this output as a new table for each result. If anyone has a better approach, I'm all ears!

Using r-markdown

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

library(qrcode)
library(knitr)

#Generate the data frame
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 requires a character

#Generate a qrcode for each test in df$test
#Requires defining an output folder and writing a new jpeg for each qrcode (not ideal)
for(i in 1:length(df$test)){

  mypath <- file.path("path/name", "qrs", paste(df$test[i], ".jpg", sep = "")) 

  jpeg(file=mypath)
  sapply(df$result.one, function(x) qrcode_gen(df$result.one[i]))
  dev.off()
}

df$QRCodes <- paste0('![]','(path/name/', df$test, '.jpg)', '{width=0.5in}') #making the width 0.5 inches so it's readable

kable(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM