Skip to content

Instantly share code, notes, and snippets.

@kohske
Created June 22, 2012 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohske/2975392 to your computer and use it in GitHub Desktop.
Save kohske/2975392 to your computer and use it in GitHub Desktop.
report generation by spin
#' # Rスクリプトからレポート生成
#' # `spin`ning in knitr package
#'
#' ### @kohske
#'
#' Rスクリプトはこちら
#' https://gist.github.com/gists/2975392
#'
#' ---
#'
#' ## 内容
#' - ごく普通のRスクリプトとroxygenスタイルコメント(`#'`)を使ってレポートを作ります。
#' - 行頭`#' `に続けて、自由にマークダウンでマークアップできます。マークダウンでマークアップ。
#' - コードの直前に行頭`#+ `でチャックオプション指定できます。
#'
#' ---
#'
#' ## 使い方
#' 1. スクリプトを書きます。
#' 2. コメントでマークダウン書きます。
#' 3. `spin`します。
#' 4. ブラウザで開きます。
#'
#' ```{r eval = FALSE, prompt = TRUE}
#' library(knitr)
#' spin('script.R')
#' browseURL(normalizePath('script.html'))
#' ```
#'
#' ---
#'
#' ## チャンクオプション例
#' こうすると
#' ```{r eval = FALSE}
#' #+ echo = TRUE
#' mean(rnorm(100))
#'
#' #+ echo = FALSE
#' sample(1:10)
#' ```
#'
#' こうなります
#+ echo = TRUE
mean(rnorm(100))
#+ echo = FALSE
sample(1:10)
#'
#' ---
#'
#' ## 図
#+ fig.width=5, fig.height=4
library(ggplot2)
qplot(rnorm(100), rnorm(100), colour = rnorm(100)) + guides(colour = "colourbar")
#'
#' ---
#'
#' ## 表
#'
#' `xtable`を使うと`data.frame`を簡単に表形式で出力可。
#' チャンクに`results = "asis"`オプションが必要。
#'
#' ```{r eval = FALSE}
#' #+ results = "asis"
#' library(xtable)
#' print(xtable(head(iris)), type="html")
#' ```
#+ echo = FALSE, results = "asis"
library(xtable)
print(xtable(head(iris)), type="html")
#' # Report generation using R script
#' # `spin`ning in knitr package
#'
#' ### @kohske
#'
#' Here is the script generating this html
#' https://gist.github.com/gists/2975392
#'
#' ---
#'
#' ## Contents
#' - Try to generate a report using R script with roxygen-style comments (`#'`).
#' - You can put any markdown comments following the line-head `#' `.
#' - You can set the chunk option by puttin the line-head `#+ `.
#'
#' ---
#'
#' ## How to use
#' 1. Write a script
#' 2. Put comments with markdown
#' 3. `spin` it.
#' 4. Open it in web browser.
#'
#' ```{r eval = FALSE, prompt = TRUE}
#' library(knitr)
#' spin('script.R')
#' browseURL(normalizePath('script.html'))
#' ```
#'
#' ---
#'
#' ## An example of chunk opttion
#' The followings will generate
#' ```{r eval = FALSE}
#' #+ echo = TRUE
#' mean(rnorm(100))
#'
#' #+ echo = FALSE
#' sample(1:10)
#' ```
#'
#' this kind of html
#+ echo = TRUE
mean(rnorm(100))
#+ echo = FALSE
sample(1:10)
#'
#' ---
#'
#' ## Figure
#+ fig.width=5, fig.height=4
library(ggplot2)
qplot(rnorm(100), rnorm(100), colour = rnorm(100)) + guides(colour = "colourbar")
#'
#' ---
#'
#' ## Table
#'
#' `xtable` is available for generating html-compatible table from `data.frame`.
#' You need `results = "asis"` in chunk option
#'
#' ```{r eval = FALSE}
#' #+ results = "asis"
#' library(xtable)
#' print(xtable(head(iris)), type="html")
#' ```
#+ echo = FALSE, results = "asis"
library(xtable)
print(xtable(head(iris)), type="html")
@kohske
Copy link
Author

kohske commented Jun 22, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment