+ - 0:00:00
Notes for current slide
Notes for next slide

RSP: A Brief Introduction

by Henrik Bengtsson, NA

Available on CRAN since 2005 (first version ~2002):

install.packages("R.rsp")
> library("R.rsp")
> rcat("A random number: <%= sample(1:10, size = 1) %>")
A random number: 7
1 / 14

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, ...
  • 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

2 / 14

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'
3 / 14

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
4 / 14

RSP Markup Language

1. RSP comments (<%-- ... --%>)

2. RSP preprocessing directives (<%@ ... %>)

3. RSP code expressions (<% ... %>)

5 / 14

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.

6 / 14

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.

7 / 14

RSP Markup Language

3. RSP code expressions (<% ... %>)

Insert value of evaluated R expressions

<%= sample(1:100, size = 1) %>


Code snippets - mixture of RSP and text

<% for (i in 1:3) { %>
Step <%= i %>.
<% } %>
8 / 14

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 ...) %>


To achieve the same using noweb-style markup (e.g. Sweave and knitr) is tedious.

9 / 14

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 ...) %>
10 / 14

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


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.
11 / 14

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


> 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'
12 / 14

Including graphics (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

13 / 14

Appendix

Session info:

R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS
Matrix products: default
BLAS: /home/henrik/shared/software/CBI/_ubuntu22_04/R-4.3.2-gcc11/lib/R/lib/libRblas.so
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
time zone: America/Los_Angeles
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R.devices_2.17.2 R.rsp_0.46.0 R.oo_1.26.0 R.methodsS3_1.8.2
loaded via a namespace (and not attached):
[1] compiler_4.3.2 tools_4.3.2 base64enc_0.1-4
[4] digest_0.6.34 R.cache_0.16.0 Cairo_1.6-2
[7] R.utils_2.12.3-9002

These slides were generated using R.rsp::rfile() in 0.06 secs.

14 / 14

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, ...
  • 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

2 / 14
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow