# 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

##  2

0.4 - 0.7 + 0.3  # what? it is not zero!

##  5.551e-17


## graphics

It is easy.

plot(1:10) hist(rnorm(1000)) ## 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", " ")[]

##  "hello"    "indented" "world"

2. bar is better

## conclusion

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

hi there

1:10

##    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


# 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  ##  -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)) }) ## 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  ##  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  ##  2  0.4 - 0.7 + 0.3 # what? it is not zero!  ##  5.551e-17  ## graphics It is easy. plot(1:10) hist(rnorm(1000)) ## 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", " ")[]  ##  "hello" "indented" "world"  2. bar is better ## conclusion Nothing fancy. You are ready to go. When you become picky, go to the knitr website. # 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  ##  2  0.4 - 0.7 + 0.3 # what? it is not zero!  ##  5.551e-17  ## graphics It is easy. plot(1:10) hist(rnorm(1000)) ## 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", " ")[]  ##  "hello" "indented" "world"  2. bar is better ## conclusion Nothing fancy. You are ready to go. When you become picky, go to the knitr website. # 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  ##  2  0.4 - 0.7 + 0.3 # what? it is not zero!  ##  5.551e-17  ## graphics It is easy. plot(1:10) hist(rnorm(1000)) ## 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", " ")[]  ##  "hello" "indented" "world"  2. bar is better ## conclusion Nothing fancy. You are ready to go. When you become picky, go to the knitr website. # 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 = 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:

## nested code chunks

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

1. foo is good

strsplit("hello indented world", " ")[]

##  "hello"    "indented" "world"

2. bar is better

## conclusion

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