How to create a Markdown document using by knitr package

R code chunk embeded

You can embed an R code chunk.

library(ggplot2)
library(sqldf)
library(tcltk)

## Data Import ##

golddata <- read.table("gold.txt", sep = ",", header = T)


## sqldf를 사용한 데이터 추출 ##

myData <- sqldf("select * from golddata where (Year >= 1900 and Year < 2012)")
# Year : 1900 ~ 2008


## log-linear ##

log.linear <- lm(log(Result) ~ Year, data = myData)
years <- seq(1896, 2012, 4)
predictions <- exp(predict(log.linear, newdata = data.frame(Year = years), level = 0.95, 
    interval = "prediction"))
# fitting value, upper bound, lower bound predict.

predictions <- data.frame(predictions)

Plot embeded

You can also embed the output of any embeded R code chunks.
ex) boxplot, line or bar graph, scatter plot, …

ggplot2

g <- ggplot(golddata, aes(x = Year, y = Result)) + geom_point(shape = 1, size = 4, 
    colour = "black") + geom_line(aes(x = years, y = predictions$fit), colour = "red") + 
    geom_line(aes(x = years, y = predictions$upr), linetype = "dashed", colour = "black") + 
    geom_line(aes(x = years, y = predictions$lwr), linetype = "dashed", colour = "black") + 
    scale_x_continuous(breaks = seq(1896, 2012, 4)) + scale_y_continuous(limits = c(9.2, 
    12)) + opts(title = "Olympic 100 meter sprint record") + opts(plot.title = theme_text(size = 25, 
    lineheight = 10, face = "bold")) + ylab("Winning time for the 100m men final (s)")

London.Prediction <- predictions$fit[length(years)]
winning.time <- golddata$Result[30]
g2 <- g + geom_point(aes(x = 2012, y = London.Prediction), size = 4, colour = "red") + 
    # predict 2012 Olympic 100m record
geom_point(aes(x = 2012, y = winning.time), size = 3, colour = "gold")
# actual 2012 olympic 100m record
g2

plot of chunk unnamed-chunk-2