<%@meta language="R-vignette" content="--------------------------------
  DIRECTIVES FOR R:

  %\VignetteIndexEntry{Introductory slides on RSP}
  %\VignetteAuthor{Henrik Bengtsson}
  %\VignetteKeyword{R}
  %\VignetteKeyword{RSP markup language}
  %\VignetteKeyword{literate programming}
  %\VignetteKeyword{reproducible research}
  %\VignetteKeyword{report generator}
  %\VignetteKeyword{Sweave}
  %\VignetteKeyword{knitr}
  %\VignetteKeyword{brew}
  %\VignetteKeyword{noweb}
  %\VignetteKeyword{TeX}
  %\VignetteKeyword{LaTeX}
  %\VignetteKeyword{Markdown}
  %\VignetteKeyword{AsciiDoc}
  %\VignetteKeyword{reStructuredText}
  %\VignetteKeyword{Org-Mode}
  %\VignetteKeyword{HTML}
  %\VignetteKeyword{PDF}
  %\VignetteEngine{R.rsp::md.rsp+knitr:pandoc}
  %\VignetteTangle{FALSE}
--------------------------------------------------------------------"%>
<% t0 <- Sys.time() %>
<%---------------------------------------------------------------------
  Usage:
  rsp <- "RSP_intro.md.rsp"
  md <- R.rsp::rfile(rsp, postprocess=FALSE)
  html <- R.rsp::RspFileProduct(knitr::pandoc(md, format="dzslides"))

  References:
  [1] http://cran.r-project.org/package=R.rsp
  [2] yihui.name/knitr/
  [3] http://paulrouget.com/dzslides/
  ---------------------------------------------------------------------%>
<%@string version="${version}"%>
<% R.utils::use("R.rsp"); %>
<% dzslides <- isCapableOf(R.rsp, "pandoc (>= 1.9.2)"); %>
<% if (dzslides) { %>
<%---------------------------------------------------------------------
  Pandoc post-processing configuration
  ---------------------------------------------------------------------%>
<%
Sys.setenv("R.rsp/pandoc/args/format"="dzslides");
%>
<!--pandoc
standalone:
smart:
title-prefix: "<%@meta name="title"%>"
<% if (isCapableOf(R.rsp, "pandoc (>= 1.12)")) { %>
M: TITLE:"<%@meta name="author"%>"
M: AUTHOR:"<%@meta name="title"%>"
<% } # if (...) %>
-->
<!--
Override default pandoc/dzslides settings
-->
<style>
h1, h2, h3 {
  text-align: left;
  margin: 0pt 0pt 0pt 0pt; /* top right bottom left */ 
}
h1 { font-size: 120%; }
h2 { font-size: 100%; }
h3 { font-size: 90%; }
section {
  font-size: 16pt;
}
h1.title { font-size: 180%; }
div.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 1ex;
  font-size: 60%;
  text-align: left;
  background-color: #F3F4F8;
  border-top: 1px solid #CCC;
}
/* The current item */
.incremental > *[aria-selected] { color: #0000F3; opacity: 1; }
li { padding-bottom: 0.8ex; }
pre { font-weight: bold; }
</style>
<% } # if (dzslides) %>
<%---------------------------------------------------------------------
  SETUP
  ---------------------------------------------------------------------%>
<%
R.utils::use("R.devices")
options("devEval/args/field"="dataURI")
devOptions("png", width=840)
%>
<%---------------------------------------------------------------------
  RSP TEMPLATES
  ---------------------------------------------------------------------%>
<% page <- 2L; maxSlide <- 15L; %>
<% slide <- function(title) { %>
# <%=title%>
<div class="footer">
<%@meta name="author"%>, <%@meta name="title"%>, <%=format(as.Date(R.rsp$date), format="%Y-%m-%d")%>.
<span style="float: right">
Slide <%=page%> <% if (!is.null(maxSlide)) { %> of <%=maxSlide%><% } %>
</span>
</div>
<% page <<- page + 1L %>
<% } # slide() %>


<%---------------------------------------------------------------------
  TITLE PAGE
  ---------------------------------------------------------------------%>
<h1 class="title"><%@meta name="title"%></h1>

<%@meta name="author"%>

<%=format(as.Date(R.rsp$date), format="%B %d, %Y")%>

Availability: `install.packages("R.rsp")` from <a href="http://cran.r-project.org/package=R.rsp">CRAN</a>

Since: 2002

<% if (!dzslides) { %>
_(The version of this document does not display as [DZSlides](http://paulrouget.com/dzslides/), because [Pandoc](http://johnmacfarlane.net/pandoc/) was not available when it was compiled.)_
<% } # if (!dzslides) %>

<br>


<%---------------------------------------------------------------------%>
<% slide("RSP: Hello world!") %>
<%---------------------------------------------------------------------%>
```
> library(R.rsp)

> rcat("A random number: <%%=sample(1:10, size=1)%%>")
A random number: 7
```


<%---------------------------------------------------------------------%>
<% slide("Objectives") %>
<%---------------------------------------------------------------------%>
* Dynamically generate documents and reports (and web/help pages).
* Quickly turn your favorite static report template into a dynamic one.
* Support _all_ formats, e.g. LaTeX, Markdown, HTML, ...
<%-- * Support multiple languages, e.g. R, bash, ... --%>
* Supplement and/or complement Sweave, knitr, ...
* Mix and match code and text however you want.
* Simplify sharing of template and output documents.
* Use for R package vignettes.

## Some usage
* PSCBS: PDF reports of copy-number segmentation results
* aroma-project.org: Interactive [Chromosome Explorer](http://aroma-project.org/data/reports/GSE8605/ACC,-XY,BPN,-XY,AVG,FLN,-XY,paired/ChromosomeExplorer.html) and [Array Explorer](http://www.aroma-project.org/data/reports/GSE8605/raw/ArrayExplorer.html)
* [Haplin GUI](http://haplin.fhi.no/default.asp?active_page_id=1) - genetic association analyses
* [googleVis](http://code.google.com/p/google-motion-charts-with-r/#Examples) - Google Chart Tools from R


<%---------------------------------------------------------------------%>
<% slide("Compiling RSP document into PDF, HTML, ...")%>
<%---------------------------------------------------------------------%>
```
> rfile("http://example.org/vignette.tex.rsp")
RspFileProduct:
Pathname: vignette.pdf
File size: 258.71 kB (264925 bytes)
Content type: application/pdf
Metadata 'title': 'Dynamic document creation using RSP'
Metadata 'keywords': 'Literate programming, HTML, PDF'
Metadata 'author': 'Henrik Bengtsson'
```



<%---------------------------------------------------------------------%>
<% slide("Very simple idea: Translate RSP to R and evaluate")%>
<%---------------------------------------------------------------------%>
## 1. RSP document
```
Title: Example
Counting:<%% for (i in 1:3) { %%>
 <%%=i%%>
<%% } %%>
```
## 2. R script
```
cat("Title: Example\nCounting:")
for (i in 1:3) {
  cat(" ")
  cat(i)
}
```
## 3. RSP output
```
Title: Example
Counting: 1 2 3
```

<%---------------------------------------------------------------------%>
<% slide("RSP Markup Language")%>
<%---------------------------------------------------------------------%>
## 1. RSP comments (`<%%-- ... --%%>`)
## 2. RSP preprocessing directives (`<%%@ ... %%>`)
## 3. RSP code expressions (`<%% ... %%>`)


<%---------------------------------------------------------------------%>
<% slide("RSP Markup Language")%>
<%---------------------------------------------------------------------%>
## 1. RSP comments (`<%%-- ... --%%>`)
```
<%%-----------------------------
  Compile to PDF:
  R.rsp::rfile("report.tex.rsp")
  ------------------------------%%>
\documentclass{report}
...
```
RSP comments drop anything within, e.g. private notes, other RSP constructor (nested comments too) and will never be part of the output.

<%---------------------------------------------------------------------%>
<% slide("RSP Markup Language")%>
<%---------------------------------------------------------------------%>
## 2. RSP preprocessing directives (`<%%@ ... %%>`)
### Including local and online files
```
<%%@include file="http://example.org/QC.tex.rsp"%%>
```

### Conditional inclusion
```
<%%@ifeq version="devel"%%>
  <%%@include file="templates/QA-devel.tex.rsp"%%>
<%%@else%%>
  Quality assessment is still under development.
<%%@endif%%>
```

### Meta data
```
<%%@meta title="Example"%%>
\hypersetup{pdftitle=<%%@meta name="title"%%>}
\title{<%%@meta name="title"%%>}
```

RSP preprocessing directives are independent of R, i.e. they would look the same with RSP for Python.

<%---------------------------------------------------------------------%>
<% slide("RSP Markup Language")%>
<%---------------------------------------------------------------------%>
## 3. RSP code expressions (`<%% ... %%>`)
### Insert value of evaluated R expressions
```
<%%= sample(1:100, size=1) %%>
```

<br>

### Code snippets - mixture of RSP and text 
```
<%% for (i in 1:3) { %%>
 <%%=i%%>
<%% } %%>
```



<%---------------------------------------------------------------------%>
<% slide("Looping over mixtures of code and text")%>
<%---------------------------------------------------------------------%>
```
<%% fit <- segmentByPairedPSCBS(data) %%>

\section{Segmentation results}

<%% for (chr in 1:23) { %%>
  \subsection{Chromosome <%%=chr%%>}

  <%% fitT <- extractChromosome(fit, chr) %%>
  PSCBS identified <%%=nbrOfSegments(fitT)%%> segments
  on Chr. <%%=chr%%>.

  ...
<%% } # for (chr ...) %%>
```

<br>

It is rather tedious to achieve the same using noweb-style markup, e.g. Sweave and knitr.



<%---------------------------------------------------------------------%>
<% slide("RSP template functions")%>
<%---------------------------------------------------------------------%>
```
<%%-- RSP TEMPLATES --%%>
<%% chromosomeSummary <- function(chr) { %%>
  <%% fitT <- extractChromosome(fit, chr) %%>
  PSCBS identified <%%=nbrOfSegments(fitT)%%> segments
  on Chr. <%%=chr%%>.
  ...
<%% } %%>

<%%-- DOCUMENT --%%>
...
<%% for (chr in 1:23) { %%>
  \subsection{Chromosome <%%=chr%%>}
  <%% chromosomeSummary(chr) %%>
<%% } # for (chr ...) %%>
```


<%---
<%---------------------------------------------------------------------%>
<% slide("RSP template functions")%>
<%---------------------------------------------------------------------%>
```
<%%-- RSP TEMPLATES --%%>
<%%@include file="http://example.org/chrSum.tex.rsp"%%>

<%%-- DOCUMENT --%%>
...
<%% for (chr in 1:23) { %%>
  \subsection{Chromosome <%%=chr%%>}
  <%% chromosomeSummary(chr) %%>
<%% } # for (chr ...) %%>
```
---%>

<%---------------------------------------------------------------------%>
<% slide("R.rsp package - RSP engine for R")%>
<%---------------------------------------------------------------------%>
## rcat() - RSP version of cat()
```
> rcat("A random integer in [1,100]:
        <%%=sample(1:100, size=1)%%>\n")
A random integer in [1,100]: 77
```

<br>

## rsource() - RSP version of source()
Consider RSP file 'count.rsp' (think 'count.R'):

```
Counting:<%% for (i in 1:10) { %%>
  <%% Sys.sleep(0.3) %%><%%=i%%>
<%% } %%>.
```
Running this RSP script gives:

```
> rsource("count.rsp")
Counting: 1 2 3 4 5 6 7 8 9 10.
```

<%---------------------------------------------------------------------%>
<% slide("rfile() - end-to-end compilation")%>
<%---------------------------------------------------------------------%>
```
> rfile("report.md.rsp", args=list(n=50, model="L1"))
RspFileProduct:
Pathname: report.html
File size: 42.54 kB (43564 bytes)
Content type: text/html
```

<br>

```
> rfile("http://example.org/vignette.tex.rsp")
RspFileProduct:
Pathname: vignette.pdf
File size: 258.71 kB (264925 bytes)
Content type: application/pdf
Metadata 'title': 'Dynamic document creation using RSP'
Metadata 'keywords': 'Literate programming, HTML, PDF'
Metadata 'author': 'Henrik Bengtsson'
```


<%---------------------------------------------------------------------%>
<% slide("Including graphics (using R.devices package)") %>
<%---------------------------------------------------------------------%>
RSP-embedded LaTeX

```
\includegraphics{<%%=toPDF("MyFigure,yeah,cool", {
 curve(dnorm, from=-5, to=+5)
})%%>}
```

generates the image file and outputs

```
\includegraphics{MyFigure,yeah,cool}
```

which appears as

<img src="<%=toPNG("MyFigure,yeah,cool", aspectRatio=0.5, {
  par(cex=2, mar=c(4,3,1,1), mgp=c(1.8,0.5,0))
  curve(dnorm, from=-5, to=+5);
})%>" width=480>


<%---------------------------------------------------------------------%>
<% slide("Appendix") %>
<%---------------------------------------------------------------------%>
Session info:

<pre style="font-size: 70%">
<% print(sessionInfo()) %>
</pre>

This report was automatically generated using `rfile()` of the R.rsp package.
Total processing time after RSP-to-R translation was <%=dt <- round(Sys.time()-t0, digits=2)%> <%=attr(dt, "units")%>.
