spin_utils.sh 902 Bytes
Newer Older
1 2

spinr(){
Holger Brandl's avatar
Holger Brandl committed
3
    ## test if present in PATH
4
    if [ -z "$(which spin.R)" ]; then
Holger Brandl's avatar
Holger Brandl committed
5
        >&2 echo "spin.R is not installed. See https://github.com/holgerbrandl/datautils/tree/master/R/spinr for details"
6 7
    fi

Holger Brandl's avatar
Holger Brandl committed
8
    spin.R $*
9
}
Holger Brandl's avatar
Holger Brandl committed
10
export -f spinr
11 12 13 14 15


spinsnip(){
    if [ $# -lt 1 ]; then
         >&2 echo "Usage: spinsnip <report name> [other args]*"
Holger Brandl's avatar
Holger Brandl committed
16
         >&2 echo "The R snippet to be spinned will be read from standard input."
17 18 19 20
        return
    fi

    reportName=$1
21
    tmpR=$(mktemp -d)/$(echo $reportName | tr " " "_").R
22 23 24 25 26 27 28

    ## http://stackoverflow.com/questions/11454343/pipe-output-to-bash-function
    cat | sed 's/#>/#'"'"'/g'  > $tmpR

    echo "spining $tmpR..."

    shift
29
    spinr -e $tmpR $*
30

31 32
#    rm -r $(dirname $tmpR)
    rm ${tmpR}
33
}
Holger Brandl's avatar
Holger Brandl committed
34
export -f spinsnip
35 36 37 38 39 40 41

## usage example
# echo '
# > # test report
# 1+1;
# ggplot(iris, aes(Sepal.Width) + geom_histogram()
# ' | spinsnip some_report