Skip to content

Latest commit

 

History

History
146 lines (126 loc) · 2.54 KB

File metadata and controls

146 lines (126 loc) · 2.54 KB

My first simple Rmd file

Don't forget to install knitr package.

install.packages("knitr")

Introduction

This is an .Rmd document. Maybe you have heard of Sweave? Well, knitr is like modern Sweave.

  • can work with markdown files (simpler) and LaTeX.
  • more flexible re: graphics
  • caching
  • fancy look and feel.

A simple example

Let's get R to do some simple maths.

x <- 1:3
y <- 4:6
outer(x, y)

##      [,1] [,2] [,3]
## [1,]    4    5    6
## [2,]    8   10   12
## [3,]   12   15   18

As well as having chunks in separate paragaphs, you can have some inline computation. For example, the mean of x is 2 and the sum of x and y is 5, 7, 9. Chunks can be named and then referred to later.

Plotting is easy too

x <- seq(from=0, to=2*pi, length=1000)
y <- cos(3*x)
plot(x, y, type='l', col='blue')

Tables

See also the xtable package.

library(knitr)
kable(head(iris[,1:3]), format='html')
Sepal.Length Sepal.Width Petal.Length
5.1 3.5 1.4
4.9 3.0 1.4
4.7 3.2 1.3
4.6 3.1 1.5
5.0 3.6 1.4
5.4 3.9 1.7

Okay, what do you need to know?

  1. What is markdown syntax? Orignally here, promoted for R by Rstudio.

  2. What can knitr do? Knitr home page