R Markdown Examples



So, for example, you might create a style.css file and then at the top of your R markdown document you would include:- title: Tips and tricks for working with images and figures in R Markdown documents output: htmldocument: css: style.css. R Markdown Cheat Sheet learn more at rmarkdown.rstudio.com rmarkdown 0.2.50 Updated: 8/14 1. Workflow R Markdown is a format for writing reproducible, dynamic reports with R. Use it to embed R code and results into slideshows, pdfs, html documents, Word files and more. To make a report.

  1. R Markdown Examples
  2. R Markdown Template Examples
  3. R Markdown Tutorial
  4. R Markdown Website Examples
4 min read2018/02/27
  • 2 TL;DR

Problem: You want to read in a data file in an R code chunk in an R Markdown post. But:

  • Where should you save the data file?
  • What file path will work to run the code chunks in the console?
  • What file path will work when you serve site?

Solution: Read on.

This post will show you how to add local data files to your blogdown site, and the file paths to read those data files in an R code chunk. (If you came here looking for how to add static images and use file paths, please see this post.)

Let’s say you have a data file called 'mazes.csv', and you want to read in that CSV file in an R chunk. The below table summarizes where the file should live in your blogdown site directory, and the file paths to use.

File locationFile path in R chunkFile path using here package
/content/post/mazes.csv'mazes.csv'no need!
/static/mazes.csv'../../static/mazes.csv'here('static', 'mazes.csv')
/static/data/mazes.csv'../../static/data/mazes.csv'here('static', 'data', 'mazes.csv')
raw GitHub url'raw_url/mazes.csv'no need!

More detail on each of these scenarios below. We’ll use readr::read_csv(), so you’ll need to install and load the readr package:

2.1 Place file in your post/ folder

What to do:

  • File goes in /content/post/mazes.csv
  • R file path is 'mazes.csv'

Example:

2.2 Place file in your static/ folder

What to do:

  • File goes in /static/mazes.csv
  • R file path is '../../static/mazes.csv'
  • R file path with here is: here('static', 'mazes.csv')

Example:

2.3 Place file in your static/data/ folder

Tutorial

R Markdown Examples

What to do:

  • File goes in /static/data/mazes.csv
  • R file path is '../../static/data/mazes.csv'
  • R file path with here is: here('static', 'data', 'mazes.csv')

Example:

2.4 Place 'mazes.csv' online

What to do:

  • File goes online: options include in a GitHub gist or push to your site’s repo
  • Use the raw GitHub or gist url

Example:

R Markdown Template Examples

How did here work?

R Markdown Tutorial

Now that is some serious black magic. Let’s break down what the here package did.

R Markdown Website Examples

For more of a breakdown, see here, here by Jenny Bryan.