spin.R 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env Rscript

# similar http://stackoverflow.com/questions/10943695/what-is-the-knitr-equivalent-of-r-cmd-sweave-myfile-rnw

#http://stackoverflow.com/questions/3433603/parsing-command-line-arguments-in-r-scripts
#https://github.com/edwindj/docopt.R
#http://www.slideshare.net/EdwindeJonge1/docopt-user2014

# load the docopt library
suppressMessages(library(docopt))

# retrieve and parse the command-line arguments
doc <- '
Use knitr to spin R documents
Holger Brandl's avatar
Holger Brandl committed
15
Usage: spin.R [options] <r_script> [<quoted_script_args>]
16 17

Options:
18
--toc     Add a table of contents
19 20 21 22 23 24 25
-c        Cache results
-e        Show Code
-w        Show warnings
-m        Show Messages
--keep    Keep generated Rmd and md files
'
#!docopt(doc, "-w test  a b c ")$keep
Holger Brandl's avatar
Holger Brandl committed
26
#spin_opts <- docopt(doc, "fastqc")
27
#docopt(doc, "-w test  a b c ")$"-w"
Holger Brandl's avatar
Holger Brandl committed
28
#spin_opts <- docopt(doc, "$DGE_HOME/dge_analysis.R \"--undirected --qcutoff 0.05 --minfpkm 2 ..\"")
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

spin_opts <- docopt(doc)
#print(spin_opts)

r_script <- spin_opts$r_script
keep_markdown_files <- as.logical(spin_opts$keep)

if(keep_markdown_files){
    print("keeping markdown files")
}

if(!file.exists(r_script)){
     stop(paste("file does not exist\n", doc))
}


require(plyr)
require(knitr)
require(stringr)

options(width=150)

#https://groups.google.com/forum/#!topic/knitr/ojcnq5Nm298

## better table css: http://www.stat.ubc.ca/~jenny/STAT545A/topic10_tablesCSS.html
Holger Brandl's avatar
Holger Brandl committed
54
commandArgs <- function(trailingOnly = FALSE){ return(as.character(spin_opts$quoted_script_args)) }
55 56 57 58
#print("trimmed args are")
#print(commandArgs())
#print("end args")

59

60
## todo use temp-file-name here to allow for cocurring spin.R in same directory --> pointless because results would be overwritten
61 62 63 64 65
## copy R-script to working directory and forget about the original one
#file.copy(r_script, basename(r_script))
system(paste("cat ", r_script," | grep -Ev '^#+$' | grep -Fv '#!/usr/bin/env Rscript' >", basename(r_script)))
r_script <- basename(r_script)

66
spin(r_script, knit=F)
67
file.remove(basename(r_script))
68 69 70

rmdScript <- str_replace(r_script, "[.]R$", ".Rmd")

71 72 73
# system(paste("mv", rmdScript, "tmp.Rmd"))
#system(paste("cat tmp.Rmd | grep -Ev '^#+$' | grep -Fv '#!/usr/bin/env Rscript' >", basename(rmdScript)))
#file.remove("tmp.Rmd")
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

cssHeader='
<style type="text/css">
  body {
      max-width: 90%;
  }
</style>


<!-- add jquery datatable support -->
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.4/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-2.1.2.min.js"></script>
<script type="text/javascript" charset="utf8" src="http://cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>
'

#<!-- add bootstrap support -->
#<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
#<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>


## custom title http://stackoverflow.com/questions/14124022/setting-html-meta-elements-with-knitr
opts_chunk$set(
    cache = spin_opts$c,
    message= spin_opts$m,
    warning= spin_opts$w,
    echo= spin_opts$e,
    fig.width=15,
    width=200
)

104 105 106 107 108 109 110 111 112
knitOptions <- c()

## optionally render toc
## see http://stackoverflow.com/questions/25872558/generating-a-table-of-contents-toc-when-using-knitrs-spin
if(spin_opts$toc){
   knitOptions= c("toc", markdown::markdownHTMLOptions(TRUE))
}

knit2html(basename(rmdScript), header=cssHeader, options = knitOptions)
113 114 115 116 117 118 119

## 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")))
}