1

I have written a script to generate an rmarkdown report that appends a QR Code containing a numerical result to a dataframe when outputting to a pdf format in kable (see my associated post here). The QC Codes are generated as a vector of images and are appended to a new column in the dataframe when the report is generated.

This works fine, however to output the table in .pdf format, I have to set kable(df, format = "markdown"), otherwise the image of the QR Code doesn't get placed in the output table, just the file path text. This works fine except if I want to modify the table using kableExtra format options, which require the kable format to be latex.

In the example code I have listed below, you can see that when kable(df, format = "markdown") the images are formatted correctly but the kableExtra formatting doesn't work. Conversely, when the kable(df, format = "latex") the kableExtra format options work, but the images do not.

---
title: "QR Code in Column"
author: "me"
date: "2019/01/20"
output: pdf_document

---

```{r mychunk, echo = FALSE, fig.path =  "qr/", results = 'asis', fig.show='hide'}
library(knitr)
library(qrcode)
library(kableExtra)

df <- data.frame(test = LETTERS[1:2],
                 result = as.character(round(rnorm(2), 2)),                    
                 stringsAsFactors = F)

res.qr <- lapply(df$result, function(qr) {
  qrcode_gen(qr)                 # create qrcodes
  nrow(qr)                       # save number of rows of df
})

path <- paste0(opts_current$get("fig.path"), opts_current$get("label"), "-")

total <- 0
df$Code <- paste0("![](", path, (1:length(res.qr)) + total, ".pdf){width=72px}")

```

```{r echo = FALSE}

# This example works to generate the desired output, but the kableExtra options won't work.
kable(df, format = "markdown") %>%
  add_header_above(c(" ", "Class 1" = 2))

```

```{r echo = FALSE}

# This example works to use the kableExtra formatting options, but the QR Code isn't an image.
kable(df, format = "latex") %>%
  add_header_above(c(" ", "Class 1" = 2))

```

I would like to be able to format the output table in the .pdf file using kableExtra, however I am open to any other options that may work!

Dorton
  • 185
  • 1
  • 2
  • 11

0 Answers0