Skip to content

Instantly share code, notes, and snippets.

@sschmutz
Created July 5, 2022 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sschmutz/f87c0c9479abb35c442a29c5e41ea995 to your computer and use it in GitHub Desktop.
Save sschmutz/f87c0c9479abb35c442a29c5e41ea995 to your computer and use it in GitHub Desktop.
add a column containing a barcode
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)
@sschmutz
Copy link
Author

sschmutz commented Jul 5, 2022

The output looks as follows:
baRcodeR_example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment