Skip to content
Snippets Groups Projects
rknit.sh 1.21 KiB
Newer Older
Holger Brandl's avatar
Holger Brandl committed


## just knit R documents as they are, no markdown is required (but supported to some extent)
rknit(){
    ## rscript=/projects/project-raphael/Rcode/misc/DivisionPerpendicularity.R
    ## rscript=/home/brandl/mnt/mack/project-raphael/Rcode/misc/DivisionPerpendicularity.R
Holger Brandl's avatar
Holger Brandl committed
    ## rscript=/Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_example.R
Holger Brandl's avatar
Holger Brandl committed
    rscript=$1
Holger Brandl's avatar
Holger Brandl committed

Holger Brandl's avatar
Holger Brandl committed
#    tmdRmd=$(mktemp  --suff .Rmd)
    tmdRmd=$(basename $rscript .R).Rmd

Holger Brandl's avatar
Holger Brandl committed
#    echo '# '$(basename $rscript .R) > $tmdRmd
Holger Brandl's avatar
Holger Brandl committed

    ## alternatively we could use the header argument for knit2html
    echo '
<style type="text/css">
Holger Brandl's avatar
Holger Brandl committed
    body {
        max-width: 80%;
    }
Holger Brandl's avatar
Holger Brandl committed
</style>
```{r}
Holger Brandl's avatar
Holger Brandl committed
    ' > $tmdRmd

#    cat $rscript | grep -Fv '#!' | Rscript --vanilla  - 'require(stringr); require(dplyr); readLines(file("stdin")) %>% str_replace("^#([#]*)> ([^{]*)([{]+.+[}])?", "```\n\\1 \\2\n```{r \\3}") %>% collapsewriteLines(stdout())' # >> $tmdRmd
    cat $rscript | grep -Fv '#!' | Rscript --vanilla -e 'source("/Users/brandl/Dropbox/Public/datautils/R/rknit/rknit_preprocessor.R")'  >> $tmdRmd

Holger Brandl's avatar
Holger Brandl committed
    echo '```' >> $tmdRmd

    echo 'require(knitr); options(width=150); opts_knit$set(cache = TRUE); knit2html("'$tmdRmd'", output="'$(basename $rscript .R)'")' | R --vanilla -q
}