Created
July 5, 2022 09:02
-
-
Save sschmutz/f87c0c9479abb35c442a29c5e41ea995 to your computer and use it in GitHub Desktop.
add a column containing a barcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(baRcodeR) # using custom_create_PDF() | |
library(pdftools) # using pdf_convert() | |
library(gt) | |
library(gtExtras) # using gt_img_rows() | |
# create example list of numbers to translate to barcodes | |
samples <- tibble(nr = c("1000000001", "1000000002", "1000000003")) | |
# for each sample in samples write a pdf containing the 1D barcode in a | |
# temporary directory using the sample name as filename | |
walk(samples$nr, | |
~ custom_create_PDF(Labels = .x, name = file.path(tempdir(), .x), | |
type = "linear", | |
numrow = 1, numcol = 1, | |
page_width = 3, page_height = 0.5, | |
width_margin = 0, height_margin = 0, | |
# a font size (Fsz) of 0 removes the written number | |
# above the barcode | |
Fsz = 0)) | |
# convert pdf created above into png which then can be displayed in a gt table | |
walk(samples$nr, | |
~ pdf_convert(pdf = file.path(tempdir(), paste0(.x, ".pdf")), | |
filenames = file.path(tempdir(), paste0(.x, ".png")), | |
format = "png", | |
dpi = 300)) | |
# show table including barcodes as images | |
samples %>% | |
mutate(barcode = file.path(tempdir(), paste0(nr, ".png"))) %>% | |
gt() %>% | |
cols_align(align = "center") %>% | |
gt_img_rows(columns = barcode, img_source = "local", height = 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output looks as follows: