Have you ever wondered about creating a QR code and adding on your R markdown report? According to Wikipedia, the QR code (abbreviated from Quick Response Code) is a type of matrix barcode designed in Japan for the automotive industry. Nowadays QR codes are scanned by a smartphone and contain numeric or alphabetic data and often link to a website or application.

In this post, I will show how to generate a QR code and add it to the header of the R markdown report. It would be advantageous to include an email address or a website link as a QR code in the PDF reports generated with R markdown before you share with collaborators.

Loading the library

library(qrcode)

In order to include the QR code in the header of the PDF report, first I will create a QR code and save as PNG file.

Create the QR code

png("qrplot.png")
qrcode_gen("https://datascienceplus.com/")
dev.off()

Add QR code in the R markdown

The code below is an R mardown document and includes in the header the graphicx and fancyhdr packages. The graphicx package serves to upload the qrplot.png we generated and the fancyhdr will place it in the header. This function \renewcommand{\headrulewidth}{0pt} is used the remove the default line that fancyhdr package adds in the header. I can place the qrplot.png in the footer as well, and to do so I will change the \fancyhead to \fancyfoot.

---
title: "PDF Output File"
output: pdf_document
header-includes:
-   \usepackage{graphicx}
-   \usepackage{fancyhdr}
-   \pagestyle{fancy}
-   \fancyhead[R]{\includegraphics[height=1cm]{images/qrplot.png}}
-   \fancyhead[L]{}
-   \fancypagestyle{plain}{\pagestyle{fancy}}
-   \renewcommand{\headrulewidth}{0pt}
---

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see .

When you click the **Knit** button a document will be generated that includes both contents as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

Successfully knitr the R markdown document will create a PDF file. A screenshot of the PDF output is presented below.

From now on, do not forget to add a QR code on your reports.