library(readxl)
library(qrcode)
library(tidyverse)
<- function(
mk_qr_label plot = 1,
field = 1,
pedigree = 'CM48',
max_len = 12,
output_dir = './output/'
){# clean inputs
<- as.character(plot)
plot <- as.character(field)
field <- as.character(pedigree)
pedigree
# info to be embedded
<- paste(c(field, '_', pedigree), collapse='')
label
# Set up save name
<- paste(plot, field, stringr::str_replace_all(label, '/', '\\'),
savename sep = '_')
<- paste(output_dir, savename, ".svg", sep = '')
save_path
# create qr code
<- qr_code(label)
code
# reshape for ggplot
<- pivot_longer(mutate(
code_long X = seq(1, nrow(as.data.frame(code))),
as.data.frame(code)),
cols = starts_with('V')) %>%
mutate(Y = -1*as.numeric(str_remove(name, 'V')))
# create text for ggplot
<- paste(
align_label c(plot,
paste0(rep(' ', max(c(0, (max_len - stringr::str_length(pedigree)))))),
field, paste0(rep(' ', max(c(0, (max_len - stringr::str_length(field)))))),
collapse = '')
pedigree),
# make plot
<- ggplot(code_long)+
plt geom_tile(aes(X, Y, fill = value))+
annotate(geom = "text",
x = min(filter(code_long, value)$X),
y = -1,
label = align_label,
hjust = 0,
size = 11
+
)scale_fill_manual(values = c('white', 'black'))+
theme_void()+
theme(legend.position = '')+
coord_equal()
return(list('qr'=plt, 'path'=save_path))
}
QR Code Generation
QR Codes for Easier Record Keeping
For certain tasks in the lab we use QR code to label plants, samples, or other materials. Generating these can take a bit of doing, so we have a few simple scripts to do this. Ideally, we want to make our labels both machine and human readable so we will produce labels with text alongside the QR code.
Use of this function is straight forward. It accepts numeric or text data for the plot, field, and pedigree of a plant. To ensure these attributes are positioned in a way that is easy to read, max_len
adds spaces after each (up to the number specified). This results in text that is separated cleanly. The function returns a plot of the QR and the expected location for it to be saved. This will enable future extensions such as writing a batch of qr codes to a word document with officer
for easy printing.
<- mk_qr_label(
output_list plot = 1,
field = 1,
pedigree = 'CM48',
max_len = 12,
output_dir = './output/'
)
print(output_list$path)
[1] "./output/1_1_1_CM48.svg"
$qr output_list
Use for batch processing is simple. We need to read in a spreadsheet with the label information and ensure that there are no missing values.Then we can loop over the entries and pass the output of mk_qr_label
into ggsave
and it will save them to the output directory.
<- read_excel('./Grain_Labels_Demo.xlsx')
labels
# overwrite key fields with character version and add in missing labels for NAs
walk(c("Plot", "Field", "Pedigree"), function(e){
<<- as.character(labels[[e]])
labels[[e]] is.na(labels[[e]]), e] <<- 'MISSING'
labels[
})
for(i in seq(1, nrow(labels))){
<- mk_qr_label(
output_list plot = labels[i, 'Plot'],
field = labels[i, 'Field'],
pedigree = labels[i, 'Pedigree'],
max_len = 12,
output_dir = './output/'
)ggsave(filename = output_list$path,
plot = output_list$qr
) }