In the previous tutorials we’ve learned about the R Markdown format and how to create a report using R Markdown in RStudio. In this tutorial, we will render or knit an R Markdown document to a web friendly, html format using the Rknitr package. knitr can be used to convert R Markdown files to many different formats including: html, pdf, GitHub markdown (.md) and more.
In this tutorial, we will render or knit an R Markdown document to a web friendly, html format using the R knitr package. Knitr can be used to convert R Markdown files to many different formats including: html, pdf, GitHub markdown (.md ) and more.
Learning Objectives
Xxiv Preface. CompileasingleRMarkdowndocumenttoareportindifferent formats,suchasPDF,HTML,orWord. Createnotebooksinwhichyoucandirectlyruncodechunksinter. R Markdown documents are converted to PDF by first converting to a TeX file and then calling the LaTeX engine to convert to PDF. By default this TeX file is removed, however if you want to keep it (e.g. For an article submission) you can specify the keeptex option. I am trying to insert a pdf image into an r markdown file. I know it is possible to insert jpg or png images. I was just wondering if it is also possible to insert a pdf image. Thanks very much!
At the end of this lesson, you will:
- Be able to produce (
knit) anhtmlfile from anR Markdownfile. - Know how to modify chuck options to change what is rendered and not rendered on the output
htmlfile.
What You Need
You will need the most current version of R and, preferably, RStudio loaded on your computer to complete this tutorial. You will also need an R Markdown document that contains a YAML header, code chunks and markdown segments.
Install R Packages
- knitr:
install.packages('knitr') - rmarkdown:
install.packages('rmarkdown')
What is Knitr?
knitr is the R package that we use to convert an R Markdown document into another, more user friendly format like .html or .pdf.
The knitr package allows us to:
- Publish & share preliminary results with collaborators.
- Create professional reports that document our workflow and results directly from our code, reducing the risk of accidental copy and paste or transcription errors.
- Document our workflow to facilitate reproducibility.
- Efficiently change code outputs (figures, files) given changes in the data, methods, etc.
The knitr package was designed to be a transparent engine for dynamic report generation with R – Yihui Xi – knitr package creator
When To Knit: Knitting is a useful exercise throughout your scientific workflow. It allows you to see what your outputs look like and also to test that your code runs without errors. The time required to knit depends on the length and complexity of the script and the size of your data.
How to Knit
To knit in RStudio, click the Knit pull down button. You want to use the Knit HTML option for this lesson.
When you click the Knit HTML button, a window will open in your console titled R Markdown. This pane shows the knitting progress. The output (html in this case) file will automatically be saved in the current working directory. If there is an error in the code, an error message will appear with a line number in the R Console to help you diagnose the problem.
Data tip: You can run knitr from the command prompt using: render(“input.Rmd”, “all”).
View the Output
When knitting is complete, the html file produced will automatically open.
Notice that information from the YAML header (title, author, date) is printed at the top of the HTML document. Then the html shows the text, code, and results of the code that you included in the Rmd document.
Challenge Activity
Add the code below to your .Rmd document. Then knit to .html format.
When you knit your .Rmd file to pdf, the plot you produce should look like the one below. Not so pretty, eh? Don’t worry - we will learn more about plotting in a later tutorial!
Where is the File?
In the steps above, we downloaded a file. However, where did that file go on your computer? Let’s find it before we go any further.
Is the boulder-precip.csv file there?
See the onlinedocumentation for additional details on using the pdf_documentformat.
R Markdown Pdflatex
Creating PDF output from R Markdown requires that LaTeX be installed.
R Markdown documents can have optional metadata that is used to generate adocument header that includes the title, author, and date. For more detailssee the documentation on R Markdown metadata.

R Markdown documents also support citations. You can find more information onthe markdown syntax for citations in theBibliographiesand Citations article in the online documentation.
Many aspects of the LaTeX template used to create PDF documents can becustomized using metadata. For example:
--- |
title: 'Crop Analysis Q3 2013' |
fontsize: 11pt |
geometry: margin=1in |
Available metadata variables include:
langDocument language code (e.g. 'es', 'fr', 'pt-BR')
fontsizeFont size (e.g. 10pt, 11pt, 12pt)
documentclassR Markdown Download
LaTeX document class (e.g. article)
classoptionOption for documentclass (e.g. oneside); may be repeated
geometryOptions for geometry class (e.g. margin=1in); may be repeated
mainfont, sansfont, monofont, mathfontDocument fonts (works only with xelatex and lualatex, see the latex_engine option)
linkcolor, urlcolor, citecolorColor for internal, external, and citation links (red, green, magenta, cyan, blue, black)
R Markdown Pdf Font
linestretchOptions for line spacing (e.g. 1, 1.5, 3)
