R Markdown Knitr

Posted on  by admin

Learning Objectives

R Markdown Knitr Not Working

Example of using R Markdown. GitHub Gist: instantly share code, notes, and snippets. When you render, R Markdown 1. Runs the R code, embeds results and text into.md file with knitr 2. Then converts the.md file into the finished format with pandoc Set a document’s default output format in the YAML header:- output: htmldocument - # Body output value creates htmldocument html pdfdocument pdf (requires Tex ).

After completing this tutorial, you will be able to:

  • Add an image to an R markdown report.
  • Describe the ideal location to store an image associated with an R markdown report so that knitr can find it when it renders a file.

What You Need

Markdown

You need R and RStudio to complete this tutorial. Also you should have an earth-analytics directory set up on your computer with a /data directory with it.

Add an Image to Your Report

You can add images to an R Markdown report using markdown syntax as follows:

However, when you knit the report, R will only be able to find your image if you have placed it in the right place - RELATIVE to your .Rmd file. This is where good file management becomes extremely important.

To make this simple, let’s set up a directory named images in your earth-analytics project / working directory. If your .Rmd file is located in the root of this directory and all images that you want to include in your report are located in the images directory within the earth-analytics directory, then the path that you would use for each image would look like:

R Markdown Knitr Options

images/week3/image-name-here.png

Let’s try it with an actual image.

And here’s what that code does IF the image is in the right place:

If all of your images are in your images directory, then knitr will be able to easily find them. This also follows good file management practices because all of the images that you use in your report are contained within your project directory.

Additional Resources

Add Citations to R Markdown Convert R Markdown to PDF or HTML