Commit 7150853b authored by Holger Brandl's avatar Holger Brandl

cosmetics

parent bb789256
......@@ -30,7 +30,6 @@ r_script <- opts$r_script
keep_markdown_files <- as.logical(opts$keep)
if(!file.exists(r_script)){
stop(paste("file does not exist\n", doc))
}
......@@ -50,16 +49,26 @@ file.copy(r_script, tmpScript)
cat(metadata, file = tmpScript, append = TRUE)
# see http://stackoverflow.com/questions/17341122/link-and-execute-external-javascript-file-hosted-on-github
jsAddons <- tempfile(fileext=".js")
#cat("<script src='https://raw.githubusercontent.com/holgerbrandl/datautils/master/R/rendr/toggle_code_sections.js' type='application/javascript'></script>", file=jsAddons)
cat("<script src='https://code.jquery.com/jquery-2.1.4.min.js' type='application/javascript'></script>", file=jsAddons)
cat("<script src='http://cdn.rawgit.com/holgerbrandl/datautils/master/R/rendr/toggle_code_sections.js' type='application/javascript'></script>", file=jsAddons, append=T)
## does not work yet
#cat("<style type='text/css'>
#.container-fluid {
# max-width: 100% ;
## color: blue;
#}
#</style>", file=jsAddons, append=T)
#require(plyr)
require(knitr)
require(stringr)
options(width=150)
#https://groups.google.com/forum/#!topic/knitr/ojcnq5Nm298
......@@ -75,19 +84,9 @@ opts_chunk$set(
message= opts$m,
warning= opts$w,
echo= opts$e,
fig.width=15,
width=200
out.width='100%'
)
rmarkdown::render(input=tmpScript,output_file=str_replace(basename(r_script), ".R", ".html"),
output_format=rmarkdown::html_document(toc = opts$toc, keep_md=T, pandoc_args=paste0("--include-in-header=", jsAddons)),
output_dir=getwd(),
output_options=list(toc="yes")
)
## also remove the .md and the .Rmd files
if(is.logical(keep_markdown_files) & !keep_markdown_files){
# file.remove(basename(rmdScript))
# file.remove(basename(str_replace(r_script, "[.]R$", ".md")))
}
output_dir=getwd())
##
rknit(){
if [ $# -ne 1 ]; then
echo -e "Usage: rknit <script.R>\nJust knit R documents as they are, no markdown is required (but supported to some extent)"
return
fi
## rscript=/Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_example.R
rscript=$1
# tmpRmd=$(mktemp --suff .Rmd)
tmpRmd=$(basename $rscript .R).Rmd
# echo '# '$(basename $rscript .R) > $tmpRmd
## alternatively we could use the header argument for knit2html
echo '
<style type="text/css">
body {
max-width: 90%;
}
</style>
```{r}
' > $tmpRmd
# cat $rscript | grep -Fv '#!' | Rscript --vanilla - 'require(stringr); require(dplyr); readLines(file("stdin")) %>% str_replace("^#([#]*)> ([^{]*)([{]+.+[}])?", "```\n\\1 \\2\n```{r \\3}") %>% collapsewriteLines(stdout())' # >> $tmpRmd
# cat $rscript | grep -Fv '#!' | Rscript --vanilla -e 'source("/Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_preprocessor.R")' >> $tmpRmd
# cat $rscript | sed 's/^#>$//g'| grep -Fv '#!' | Rscript --vanilla -e 'devtools::source_url("https://raw.githubusercontent.com/holgerbrandl/datautils/master/R/rknit/rknit_preprocessor.R")' | grep -v "^#>" >> $tmpRmd
cat $rscript | sed 's/^#>$//g' | grep -Fv '#!'| grep -Ev '^#+$' | Rscript --vanilla -e 'devtools::source_url("https://raw.githubusercontent.com/holgerbrandl/datautils/v1.9/R/rknit/rknit_preprocessor.R")' >> $tmpRmd
echo '```' >> $tmpRmd
echo 'require(knitr); options(width=150); opts_chunk$set(cache = TRUE, fig.width=10, width=100); knit2html("'$tmpRmd'", output="'$(basename $rscript .R)'")' | R --vanilla -q
rm $(basename $rscript .R) $tmpRmd
}
## notes
# http://yihui.name/knitr/options --> strip.white:
#> {message=F, echo=F}
require(dplyr)
require(ggplot2)
##> test heading
#> ... followed by some markup
head(iris)
#> some markup
#> * or list
#> * elements
#>
#> test
ggplot(iris, aes(Species, Sepal.Length, fill=Species)) + geom_boxplot() + scale_fill_discrete(guide=F)
#> {fig.width=14}
## just chunk definition without markdown text or header
ggplot(iris, aes(Species, Sepal.Length, fill=Species)) + geom_boxplot() + scale_fill_discrete(guide=F)
# source /Users/brandl/Dropbox/Public/datautils/R/rknit/rknit.sh
# rknit /Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_example.R
require(stringr);
require(dplyr);
#readLines(file("stdin")) %>%
mdRegex <- "^#([#]*)> ([^{]*)([{]+(.+)[}])?"
#str_match("##> test heading", mdRegex)
#str_match("##> test heading {fig.width=10}", mdRegex)
#str_match("#> simple markup", mdRegex)
#str_match("#> {fig.width=10}", mdRegex)
#str_replace("#> * normBxx >0 --> horizontal", mdRegex, "```\n\\1\\2\n```{r \\4}") %>% paste(collapse="\n") %>% cat()
#readLines(file("/Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_example.R")) %>%
readLines(file("stdin")) %>%
str_replace(mdRegex, "```\n\\1 \\2\n```{r \\4}") %>% paste(collapse="\n") %>% cat()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment