Henrik Bengtsson
December 18, 2015
Availability: install.packages("R.rsp")
from CRAN
Since: 2002
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 2 of 15
> library(R.rsp)
> rcat("A random number: <%=sample(1:10, size=1)%>")
A random number: 7
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 3 of 15
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 4 of 15
> 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'
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 5 of 15
Title: Example
Counting:<% for (i in 1:3) { %>
<%=i%>
<% } %>
cat("Title: Example\nCounting:")
for (i in 1:3) {
cat(" ")
cat(i)
}
Title: Example
Counting: 1 2 3
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 6 of 15
<%-- ... --%>
)<%@ ... %>
)<% ... %>
)Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 7 of 15
<%-- ... --%>
)<%-----------------------------
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.
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 8 of 15
<%@ ... %>
)<%@include file="http://example.org/QC.tex.rsp"%>
<%@ifeq version="devel"%>
<%@include file="templates/QA-devel.tex.rsp"%>
<%@else%>
Quality assessment is still under development.
<%@endif%>
<%@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.
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 9 of 15
<% ... %>
)<%= sample(1:100, size=1) %>
<% for (i in 1:3) { %>
<%=i%>
<% } %>
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 10 of 15
<% 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 ...) %>
It is rather tedious to achieve the same using noweb-style markup, e.g. Sweave and knitr.
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 11 of 15
<%-- 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 ...) %>
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 12 of 15
> rcat("A random integer in [1,100]:
<%=sample(1:100, size=1)%>\n")
A random integer in [1,100]: 77
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.
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 13 of 15
> 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'
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 14 of 15
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
Henrik Bengtsson, Introductory slides on RSP, 2015-12-18. Slide 15 of 15
Session info:
R version 3.2.3 Patched (2015-12-14 r69777) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=C [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] markdown_0.7.7 R.devices_2.13.2 R.rsp_0.21.0 R.oo_1.19.0 [5] R.methodsS3_1.7.0 loaded via a namespace (and not attached): [1] tools_3.2.3 base64enc_0.1-3 digest_0.6.8 [4] R.cache_0.12.0-9000 Cairo_1.5-9 R.utils_2.2.0
This report was automatically generated using rfile()
of the R.rsp package. Total processing time after RSP-to-R translation was 4.56 secs.