Skip to content
Snippets Groups Projects
Commit b83b9ef5 authored by Holger Brandl's avatar Holger Brandl
Browse files

started improved spin shell wrapper

parent ee523efc
No related branches found
No related tags found
No related merge requests found
A tiny wrapper around knitr::spin to use it from the terminal
===
To prepare a shell source in the script
source <(curl https://dl.dropboxusercontent.com/u/113630701/datautils/bash/bioinfo_utils.sh 2>&1 2>/dev/null)
To use them just source them when needed.
Use the issue tracker to suggest changes or to report problems
Bash
===
LSF Cluster Utils:
```
source <(curl https://dl.dropboxusercontent.com/u/113630701/datautils/bash/lsf_utils.sh 2>&1 2>/dev/null)
```
Tools to simplify bio-dataprocessing in bash
```
```
R
===
The R bits are split up into tools for
* general data handling
* plotting using ggplot2
* bioinformatics using various bioconductor packages
```
devtools::source_url("https://dl.dropboxusercontent.com/u/113630701/datautils/R/core_commons.R")
devtools::source_url("https://dl.dropboxusercontent.com/u/113630701/datautils/R/ggplot_commons.R")
devtools::source_url("https://dl.dropboxusercontent.com/u/113630701/datautils/R/bio/bioinfo_commons.R")
devtools::source_url("https://dl.dropboxusercontent.com/u/113630701/datautils/R/datatable_commons.R")
```
#!/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
## a thin wrapper around spin to make it more useful with more custom output
#devtools::source_url("https://dl.dropboxusercontent.com/u/113630701/datautils/R/core_commons.R")
# load the docopt library
suppressMessages(library(docopt))
# retrieve and parse the command-line arguments
doc <- '
Use knitr to spin R documents
Usage: spin.R [options] <r_script> [<script_args>...]
Options:
-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
#docopt(doc, "-w test a b c ")$"-w"
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
commandArgs <- function(trailingOnly = FALSE){ return(as.character(spin_opts$script_args)) }
#print("trimmed args are")
#print(commandArgs())
#print("end args")
spin(r_script, knit=F)
rmdScript <- str_replace(r_script, "[.]R$", ".Rmd")
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")
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
)
knit2html(basename(rmdScript), header=cssHeader)
## 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")))
}
spinr(){
## download if not yet there
if [ -z "$(which spin.R)" ]; then
wget -P ~/ https://raw.githubusercontent.com/holgerbrandl/themoviedbapi/v1.0/LICENCE.txt
fi
~/spin.R $*
}
spinsnip(){
if [ $# -lt 1 ]; then
>&2 echo "Usage: spinsnip <report name> [other args]*"
return
fi
reportName=$1
tmpR=$(echo $reportName | tr " " "_").R
## http://stackoverflow.com/questions/11454343/pipe-output-to-bash-function
cat | sed 's/#>/#'"'"'/g' > $tmpR
echo "spining $tmpR..."
shift
spinr $tmpR $*
rm $tmpR
}
## usage example
# echo '
# > # test report
# 1+1;
# ggplot(iris, aes(Sepal.Width) + geom_histogram()
# ' | spinsnip some_report
Files in the directory should not be used any longer. Use ../spinr instead
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment