This is a cool blog post from useR2014

A minimal R Markdown example

A quote:

Markdown is not LaTeX.

To compile me, run this in R:

library(knitr)
knit('001-minimal.Rmd')

See output here.

code chunks

A paragraph here. A code chunk below (remember the three backticks):

1 + 1
## [1] 2
0.4 - 0.7 + 0.3  # what? it is not zero!
## [1] 5.551e-17

graphics

It is easy.

plot(1:10)

plot of chunk unnamed-chunk-2

hist(rnorm(1000))

plot of chunk unnamed-chunk-2

inline code

Yes I know the value of pi is 3.1416, and 2 times pi is 6.2832.

math

Sigh. You cannot live without math equations. OK, here we go: \(\alpha+\beta=\gamma\). Note this is not supported by native markdown. You probably want to try RStudio, or at least the R package markdown, or the function knitr::knit2html().

nested code chunks

You can write code within other elements, e.g. a list

  1. foo is good
    strsplit("hello indented world", " ")[[1]]
    
    ## [1] "hello"    "indented" "world"
    
  2. bar is better

conclusion

Nothing fancy. You are ready to go. When you become picky, go to the knitr website.

knitr logo

Test one

hi there

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
summary(lm(y ~ x, data = data.frame(x = 1:10, y = rnorm(10))))
## 
## Call:
## lm(formula = y ~ x, data = data.frame(x = 1:10, y = rnorm(10)))
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.432 -1.226  0.259  0.805  2.853 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  -0.1142     1.1735   -0.10     0.92
## x             0.0993     0.1891    0.53     0.61
## 
## Residual standard error: 1.72 on 8 degrees of freedom
## Multiple R-squared:  0.0333, Adjusted R-squared:  -0.0875 
## F-statistic: 0.276 on 1 and 8 DF,  p-value: 0.614

Do categories work?

A Minimal Example for Markdown

This is a minimal example of using knitr to produce an HTML page from Markdown.

R code chunks

# set global chunk options: images will be 7x5 inches
opts_chunk$set(fig.width = 7, fig.height = 5)

Now we write some code chunks in this markdown file:

x <- 1 + 1  # a simple calculator
set.seed(123)
rnorm(5)  # boring random numbers
## [1] -0.56048 -0.23018  1.55871  0.07051  0.12929

We can also produce plots:

par(mar = c(4, 4, 0.1, 0.1))
with(mtcars, {
    plot(mpg ~ hp, pch = 20, col = "darkgray")
    lines(lowess(hp, mpg))
})

plot of chunk graphics

Inline code

Inline R code is also supported, e.g. the value of x is 2, and 2 × π
= 6.2832.

Math

LaTeX math as usual: \(f(\alpha, \beta) \propto x^{\alpha-1}(1-x)^{\beta-1}\).

Misc

You can indent code chunks so they can nest within other environments such as lists.

  1. the area of a circle with radius x

    pi * x^2
    
    ## [1] 12.57
    
  2. OK, that is great

To compile me, use

library(knitr)
knit("knitr-minimal.Rmd")

Conclusion

Markdown is super easy to write. Go to knitr homepage for details.

yet another test using opts_knit

A minimal R Markdown example

A quote:

Markdown is not LaTeX.

To compile me, run this in R:

library(knitr)
knit('001-minimal.Rmd')

See output here.

code chunks

A paragraph here. A code chunk below (remember the three backticks):

1 + 1
## [1] 2
0.4 - 0.7 + 0.3  # what? it is not zero!
## [1] 5.551e-17

graphics

It is easy.

plot(1:10)

plot of chunk unnamed-chunk-2

hist(rnorm(1000))

plot of chunk unnamed-chunk-2

inline code

Yes I know the value of pi is 3.1416, and 2 times pi is 6.2832.

math

Sigh. You cannot live without math equations. OK, here we go: $\alpha+\beta=\gamma$. Note this is not supported by native markdown. You probably want to try RStudio, or at least the R package markdown, or the function knitr::knit2html().

nested code chunks

You can write code within other elements, e.g. a list

  1. foo is good

    strsplit("hello indented world", " ")[[1]]
    
    ## [1] "hello"    "indented" "world"
    
  2. bar is better

conclusion

Nothing fancy. You are ready to go. When you become picky, go to the knitr website.

knitr logo

yet another test using opts_knit

A minimal R Markdown example

A quote:

Markdown is not LaTeX.

To compile me, run this in R:

library(knitr)
knit('001-minimal.Rmd')

See output here.

code chunks

A paragraph here. A code chunk below (remember the three backticks):

1 + 1
## [1] 2
0.4 - 0.7 + 0.3  # what? it is not zero!
## [1] 5.551e-17

graphics

It is easy.

plot(1:10)

plot of chunk unnamed-chunk-2

hist(rnorm(1000))

plot of chunk unnamed-chunk-2

inline code

Yes I know the value of pi is 3.1416, and 2 times pi is 6.2832.

math

Sigh. You cannot live without math equations. OK, here we go: $\alpha+\beta=\gamma$. Note this is not supported by native markdown. You probably want to try RStudio, or at least the R package markdown, or the function knitr::knit2html().

nested code chunks

You can write code within other elements, e.g. a list

  1. foo is good

    strsplit("hello indented world", " ")[[1]]
    
    ## [1] "hello"    "indented" "world"
    
  2. bar is better

conclusion

Nothing fancy. You are ready to go. When you become picky, go to the knitr website.

knitr logo

another test

A minimal R Markdown example

A quote:

Markdown is not LaTeX.

To compile me, run this in R:

library(knitr)
knit('001-minimal.Rmd')

See output here.

code chunks

A paragraph here. A code chunk below (remember the three backticks):

1 + 1
## [1] 2
0.4 - 0.7 + 0.3  # what? it is not zero!
## [1] 5.551e-17

graphics

It is easy.

plot(1:10)

plot of chunk unnamed-chunk-2

hist(rnorm(1000))

plot of chunk unnamed-chunk-2

inline code

Yes I know the value of pi is 3.1416, and 2 times pi is 6.2832.

math

Sigh. You cannot live without math equations. OK, here we go: $\alpha+\beta=\gamma$. Note this is not supported by native markdown. You probably want to try RStudio, or at least the R package markdown, or the function knitr::knit2html().

nested code chunks

You can write code within other elements, e.g. a list

  1. foo is good

    strsplit("hello indented world", " ")[[1]]
    
    ## [1] "hello"    "indented" "world"
    
  2. bar is better

conclusion

Nothing fancy. You are ready to go. When you become picky, go to the knitr website.

knitr logo

No syntax highlighting

Suppress super long output

First we redefine the output hook:

# the default output hook
hook_output = knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
    if (!is.null(n <- options$out.lines)) {
        x = unlist(stringr::str_split(x, "\n"))
        if (length(x) > n) {
            # truncate the output
            x = c(head(x, n), "....\n")
        }
        x = paste(x, collapse = "\n")  # paste first n lines together
    }
    hook_output(x, options)
})

And we do not want the output to be more than 4 lines, so we set this option globally:

opts_chunk$set(out.lines = 4)

Test the new output hook:

1 + 1  # this is short
## [1] 2

The output of this chunk is truncated:

1:100
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
....

Another test of knitr and WordPress

Testing text output

See if chunk options like tidy, prompt and echo, etc work as expected.

A normal chunk

1 + 1
## [1] 2
for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989

Do not evaluate

1 + 1
for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}
# two blank lines below


dnorm(0)

Add prompts

> 1 + 1
## [1] 2
> for (i in 1:10) {
+     # nothing before 10
+     if (i >= 10) 
+         print(i)
+ }
## [1] 10
> # two blank lines below
> 
> 
> dnorm(0)
## [1] 0.3989

No evaluate or tidy

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Do not tidy

1+1
## [1] 2
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989

Do not echo

## [1] 2
## [1] 10
## [1] 0.3989

Do not comment out results

1 + 1
[1] 2
for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}
[1] 10
# two blank lines below


dnorm(0)
[1] 0.3989

Do not echo the 2nd expression

1 + 1
## [1] 2
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989

Do not evaluate, echo the 2nd expression

for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}

Only evaluate the first two expressions

1 + 1
## [1] 2
for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}
## [1] 10
## # two blank lines below
## 
## 
## dnorm(0)

Add prompts but no tidy

> 1+1
## [1] 2
> for (i in 1:10) {
+ # nothing before 10
+ if(i>=10)print(i)
+ }
## [1] 10
> # two blank lines below
> 
> 
> dnorm(0)
## [1] 0.3989

Change prompts

options(prompt = "R> ", continue = "+  ")
R> 1 + 1
## [1] 2
R> for (i in 1:10) {
+      # nothing before 10
+      if (i >= 10) 
+          print(i)
+  }
## [1] 10
R> # two blank lines below
R> 
R> 
R> dnorm(0)
## [1] 0.3989

Backslashes

{
    # can you deal with \code{foo} or \n, \a?
    gsub("\\.", "\\\\", "a.b.c")  # \link{bar}
}
## [1] "a\\b\\c"
cat("a\tb\nc")
## a    b
## c

Other formatR options

We can set formatR options globally:

options(keep.blank.line = FALSE)
1 + 1
for (i in 1:10) {
    # nothing before 10
    if (i >= 10) 
        print(i)
}
# two blank lines below
dnorm(0)

Or locally in one chunk via tidy.opts. Do not keep comments:

1 + 1
for (i in 1:10) {
    if (i >= 10) 
        print(i)
}
dnorm(0)

Move left braces to the next line:

for (i in 1:10)
{
    # nothing before 10
    if (i >= 10) 
        print(i)
}
## [1] 10

Indent by 2 spaces:

1 + 1
for (i in 1:10) {
  # nothing before 10
  if (i >= 10) 
    print(i)
}
# two blank lines below
dnorm(0)

See http://yihui.name/formatR for details.

Empty chunks

A test of knitr and WordPress

A minimal R Markdown example

A quote:

Markdown is not LaTeX.

To compile me, run this in R:

library(knitr)
knit('001-minimal.Rmd')

See output here.

code chunks

A paragraph here. A code chunk below (remember the three backticks):

1 + 1
## [1] 2
0.4 - 0.7 + 0.3  # what? it is not zero!
## [1] 5.551e-17

graphics

It is easy.

plot(1:10)

plot of chunk unnamed-chunk-2

hist(rnorm(1000))

plot of chunk unnamed-chunk-2

inline code

Yes I know the value of pi is 3.1416, and 2 times pi is 6.2832.

math

Sigh. You cannot live without math equations. OK, here we go: $\alpha+\beta=\gamma$. Note this is not supported by native markdown. You probably want to try RStudio, or at least the R package markdown, or the function knitr::knit2html().

nested code chunks

You can write code within other elements, e.g. a list

  1. foo is good

    strsplit("hello indented world", " ")[[1]]
    
    ## [1] "hello"    "indented" "world"
    
  2. bar is better

conclusion

Nothing fancy. You are ready to go. When you become picky, go to the knitr website.

knitr logo