Testing R Markdown (.Rmd)


Testing R Markdown (.Rmd)

The content on this page was generated with R code embedded in an Rmd file.

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 http://rmarkdown.rstudio.com.

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

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
fit <- lm(dist ~ speed, data = cars)
fit
## 
## Call:
## lm(formula = dist ~ speed, data = cars)
## 
## Coefficients:
## (Intercept)        speed  
##     -17.579        3.932

You can also embed plots. See Figure 1 for example:

par(mar = c(0, 1, 0, 1))
pie(
  c(280, 60, 20),
  c('Sky', 'Sunny side of pyramid', 'Shady side of pyramid'),
  col = c('#0292D8', '#F7EA39', '#C4B632'),
  init.angle = -50, border = NA
)
A fancy pie chart.

Figure 1: A fancy pie chart.

Markdown vs. Rmarkdown vs. Rmd

This section is included in three test files for three different markdown formats (.md, .Rmarkdown and .Rmd).

Some differences are:

  • You cannot run any R code in a plain md document, whereas in Rmarkdown and Rmd documents, you can embed R code chunks (```{r});
print("This text was written in R")
## [1] "This text was written in R"
  • Markdown and Rmarkdown posts are rendered through Blackfriday, and an Rmd document is compiled by rmarkdown and Pandoc.

    There are many differences in syntax between Blackfriday’s Markdown and Pandoc’s Markdown. For example, you can write a task list with Blackfriday but not with Pandoc:

    • [x] Write an R package.
    • [ ] Write a book.
    • [ ] …
    • [ ] Profit!
  • Inline Math with \\( \\) rendered with Katex in all file formats: For any \( (x,y,z) \), if \( x^2 + y^2 + z^2 = R^2 \) then \( (x,y,z) \) is on the sphere, otherwise it’s not.

  • Dedicated line of Math with \\[ \\] rendered with Katex in all file formats:

\[ (x-x_0)^2 + (y-y_0)^2 + (z-z_0)^2 = R^2 \]

  • Inline Math with $ $ rendered only in rmd: $ S_n = _{i=1}^n X_i $

  • Dedicated line of Math with $$ $$ rendered only in rmd.

\[ S_n = \sum_{i=1}^n X_i \]

  • Katex should support $ and $$ in md and Rmarkdown files as well, but I couldn’t make this work yet.

  • Note about previous MathJax implementation: Similarly, Blackfriday does not support LaTeX math and Pandoc does. you have to include math expressions in a pair of backticks (inline: `$ $`; display style: `$$ $$`), e.g., $S_n = \sum_{i=1}^n X_i$.1 For R Markdown posts, you do not need the backticks, because Pandoc can identify and process math expressions.

Other R Code

1 + 1  # not executed
1 + 1  
## [1] 2
# ggplot2 examples
library(ggplot2)
#use color brewer as default discrete colors
scale_colour_discrete <- function(...) scale_color_brewer(palette="Set1", ...)
scale_fill_discrete <- function(...) scale_fill_brewer(palette="Set1", ...)

data('mtcars')
# create factors with value labels
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
     labels=c("3gears","4gears","5gears"))
mtcars$am <- factor(mtcars$am,levels=c(0,1),
     labels=c("Automatic","Manual"))
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
   labels=c("4cyl","6cyl","8cyl"))
head(mtcars)
##                    mpg  cyl disp  hp drat    wt  qsec vs        am   gear
## Mazda RX4         21.0 6cyl  160 110 3.90 2.620 16.46  0    Manual 4gears
## Mazda RX4 Wag     21.0 6cyl  160 110 3.90 2.875 17.02  0    Manual 4gears
## Datsun 710        22.8 4cyl  108  93 3.85 2.320 18.61  1    Manual 4gears
## Hornet 4 Drive    21.4 6cyl  258 110 3.08 3.215 19.44  1 Automatic 3gears
## Hornet Sportabout 18.7 8cyl  360 175 3.15 3.440 17.02  0 Automatic 3gears
## Valiant           18.1 6cyl  225 105 2.76 3.460 20.22  1 Automatic 3gears
##                   carb
## Mazda RX4            4
## Mazda RX4 Wag        4
## Datsun 710           1
## Hornet 4 Drive       1
## Hornet Sportabout    2
## Valiant              1
# cool-plot, fig.width='80%', fig.cap='A cool plot.'}
qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5),
   main="Distribution of Gas Milage", xlab="Miles Per Gallon",
   ylab="Density")

#data(mtcars)
#plot(mtcars, pch = 20)  # not really cool
#library(car) 
#scatterplot(mpg ~ wt | cyl, data=mtcars, 
#   xlab="Weight of Car", ylab="Miles Per Gallon", 
#   main="Enhanced Scatter Plot", 
#   labels=row.names(mtcars))

Markdown Examples

Paragraph 1: Md is rendered through Blackfriday. Hard line break with two spaces:
Rmd is rendered through rmarkdown, bookdown, Pandoc.

Paragraph 2: Blank line in betweens

Setext Headers

Level 1 Header

Level 2 Header

ATX Style headers

Level 1 Header

Level 2 Header

Level 3 Header

199
 // GetTitleFunc returns a func that can be used to transform a string to // title case. // // The supported styles are // // - Go (strings.Title) // - AP (see <a href="https://www.apstylebook.com/" class="uri">https://www.apstylebook.com/</a>) // - Chicago (see <a href="http://www.chicagomanualofstyle.org/home.html" class="uri">http://www.chicagomanualofstyle.org/home.html</a>) // // If an unknown or empty style is provided, AP style is what you get. func GetTitleFunc(style string) func(s string) string { switch strings.ToLower(style) { case go: return strings.Title case chicago: tc := transform.NewTitleConverter(transform.ChicagoStyle) return tc.Title default: tc := transform.NewTitleConverter(transform.APStyle) return tc.Title } } 

A link and an emphasis

A header with identifier

Creates a div id=“foo” around the h1.

A header with class identifiers

creates div with id=“gerrit”" classes “section level1 class class” and attributes hello=“world” name=“unicone”

Header identifiers in HTML

And a link to Header identifiers in HTML

or the section on header identifiers

instead of giving id explicitly Header identifiers in HTML


  1. This is because we have to protect the math expressions from being interpreted as Markdown. You may not need the backticks if your math expression does not contain any special Markdown syntax such as underscores or asterisks, but it is always a safer choice to use backticks. When you happen to have a pair of literal dollar signs inside the same element, you can escape one dollar sign, e.g., \$50 and $100 renders “$50 and $100”.