rendr_utils.sh 902 Bytes
Newer Older
Holger Brandl's avatar
Holger Brandl committed
1
2
3
4

rendr_snippet(){
    if [ $# -lt 1 ]; then
         >&2 echo "Usage: rendr_snippet <report name> [other args]*"
Holger Brandl's avatar
Holger Brandl committed
5
         >&2 echo "The R snippet to be rendered will be read from standard input."
Holger Brandl's avatar
Holger Brandl committed
6
7
8
        return
    fi

Holger Brandl's avatar
Holger Brandl committed
9
10
11
12
13
14

    ## test if present in PATH
    if [ -z "$(which rendr.R)" ]; then
        >&2 echo "rendr.R is not installed. See https://github.com/holgerbrandl/datautils/tree/master/R/rendr for details"
    fi

Holger Brandl's avatar
Holger Brandl committed
15
    reportName=$1
Holger Brandl's avatar
Holger Brandl committed
16
    tmpR=$(mktemp -d /tmp/rendr.XXXX)/$(echo $reportName | tr " " "_").R
Holger Brandl's avatar
Holger Brandl committed
17
18
19
20
21
22
23

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

    echo "rendering $tmpR..."

    shift
Holger Brandl's avatar
Holger Brandl committed
24
    rend.R -e $tmpR $*
Holger Brandl's avatar
Holger Brandl committed
25
26
27
28

#    rm -r $(dirname $tmpR)
    rm ${tmpR}
}
Holger Brandl's avatar
Holger Brandl committed
29
export -f rendr_snippet
Holger Brandl's avatar
Holger Brandl committed
30
31
32
33
34
35

## usage example
# echo '
# > # test report
# 1+1;
# ggplot(iris, aes(Sepal.Width) + geom_histogram()
Holger Brandl's avatar
Holger Brandl committed
36
# ' | rendr_snippet some_report