Rdevel <- "~/R/r-devel/BUILD/bin/Rscript"
R <- "/usr/bin/Rscript"
R2.15.3 <- "/scratch/R-releases/R-2.15.3/bin/R"
R3.0.0 <- "/scratch/R-releases/R-3.0.0/bin/R"
library(whisker)
templateRhtml <- readLines("template.Rhtml")
templateOldRhtml <- readLines("template-old-R-version.Rhtml")
# Function to ...
#
# generate .Rhtml fragment from 'code',
# run R (as specified by 'r'),
# knit the .Rhtml fragment, then
# return the knitted HTML fragement,
# with wrapper HTML reporting the R version
# and the plot produced by the .Rhtml fragment
#
# echo=TRUE means that the code will be shown
# eval=TRUE means that the code will be run
# fig=TRUE means that the code will (also) be run to generate SVG output
# DEFAULT is to show code, NOT run the code, and generate SVG
Rsession <- function(r, label, code, echo=TRUE, eval=FALSE, fig=TRUE) {
# Generate .Rhtml doc from code
data <- list(label=label, code=code,
echo=echo, eval=eval, figeval=fig)
writeLines(whisker.render(templateRhtml, data),
paste0(label, ".Rhtml"))
system(paste(r,
paste0("-e 'library(\"knitr\"); knit(\"",
label, ".Rhtml\")'")),
ignore.stdout=TRUE, ignore.stderr=TRUE)
if (fig) {
figureHTML <- paste0('')
} else {
figureHTML <- ""
}
html <- c('
',
'',
readLines("version.txt", warn=FALSE),
' |
',
'',
readLines(paste0(label, ".html")),
' | ',
'',
figureHTML,
' | ',
'
')
cat(html, sep="\n")
}
# Function to ...
# cat code to file
# BATCH run file in old R version
# generate .Rhtml with just echoed code and echoed output
# knit the .Rhtml with current R
# WILL ONLY WORK for very simple script with single output
oldRsession <- function(r, label, code) {
writeLines(c("pdf()", paste0('sink("', label, '.out")'), code),
paste0(label, ".R"))
writeLines('sink("version.out")\ncat(R.version.string)', "version.R")
system(paste0(r, "script ", label, ".R"))
system(paste0(r, "script version.R"))
output <- paste(readLines(paste0(label, ".out"), warn=FALSE), collapse="\n")
data <- list(code=code, output=output)
writeLines(whisker.render(templateOldRhtml, data),
paste0(label, ".Rhtml"))
system(paste(R,
paste0("-e 'library(\"knitr\"); knit(\"",
label, ".Rhtml\")'")),
ignore.stdout=TRUE, ignore.stderr=TRUE)
html <- c('',
'',
readLines("version.out", warn=FALSE),
' |
',
'',
readLines(paste0(label, ".html")),
' | ',
'',
' | ',
'
')
cat(html, sep="\n")
}