NEON EDUCATION bio photo

NEON EDUCATION

Devoted to open data and open source in science and education.

View All Tutorials

This tutorial is a part of a series!

Click below to view all lessons in the series!

Tags

R programming (56)
Hierarchical Data Formats (HDF5) (15)
Spatial Data & GIS (22)
LiDAR (10)
Raster Data (14)
Remote Sensing (25)
Data Visualization (4)
Hyperspectral Remote Sensing (18)
Time Series (17)
Phenology (8)
Vector Data (6)
Metadata (1)
Git & GitHub (7)
(1) (1) (14) (1) (1) (1) (1)

Tutorial by R Package

dplyr (9)
ggplot2 (18)
h5py (2)
lubridate (time series) (7)
maps (1)
maptools (1)
plyr (2)
raster (26)
rasterVis (raster time series) (3)
rgdal (GIS) (24)
rgeos (2)
rhdf5 (11)
sp (5)
scales (4)
gridExtra (4)
ggtheme (0)
grid (2)
reshape2 (3)
plotly (5)

View ALL Tutorial Series




Twitter Youtube Github


Blog.Roll

R Bloggers

Overview

In this tutorial, we will cover the R knitr package that is used to convert R Markdown into a rendered document (HTML, PDF, etc).

Learning Objectives

At the end of this activity, you will:

  • Be able to produce (‘knit’) an HTML file from a R Markdown file.
  • Know how to modify chuck options to change the output in your HTML file.

Things You’ll Need To Complete This Tutorial

You will need the most current version of R and, preferably, RStudio loaded on your computer to complete this tutorial.

Install R Packages

  • knitr: install.packages("knitr")
  • rmarkdown: install.packages("rmarkdown")

Share & Publish Results Directly from Your Code!

The knitr package allow 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.

Publish from Rmd files with knitr

To complete this tutorial you need:

  1. The R knitr package to complete this tutorial. If you need help installing packages, visit the R packages tutorial.
  2. An R Markdown document that contains a YAML header, code chunks and markdown segments. If you don’t have an .Rmd file, visit the R Markdown tutorial to create one.

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

Location of the knit button in RStudio in Version 0.99.486. Source: National Ecological Observatory Network (NEON)

To knit in RStudio, click the knit pull down button. You want to use the
knit HTML 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”).

Activity: Knit Script

Knit the .Rmd file that you built in the last tutorial. What does it look like?

View the Output

R Markdown (left) and the resultant HTML (right) after knitting. Source: National Ecological Observatory Network (NEON)

When knitting is complete, the new 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.

Data Institute Participants: Complete Week 2 Assignment

  • Read this week’s assignment closely. Edit your RMD document to ensure that you have completed all of the steps required by assignment.
  • Be sure to carefully check your knitr output to make sure it is rendering the way you think it should!
  • When you are complete, submit your .Rmd and .html files to the NEON Institute participants GitHub repository (NEONScience/DI-NEON-participants).
  • The files will have automatically saved to your R working directory, you will need to transfer the files to the /participants/pre-institute3-rmd/ directory and submitted via a pull request.

Data Tip: If you are a frequent user of LaTex, you might find this video from the creator of knitr informational. It introduces R Markdown & knitr in conjunction with LaTex and other formats.