Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rio dimensions #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions tools/Rio
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Rio: Load CSV from stdin into R as a data.frame, execute given commands, and get the output as CSV or PNG on stdout
# Rio: Load CSV from stdin into R as a data.frame, execute given commands, and get the output as CSV, PNG or PDF on stdout
#
# Example usage:
# $ < seq 100 | Rio -nf sum (same as Rio -ne 'sum(df)')
Expand Down Expand Up @@ -30,13 +30,16 @@ OPTIONS:
-r Import dplyr and tidyr
-s Import sqldf
-b Use same settings as used for book Data Science at the Command Line
-p Export graphic as PDF
-w set width of graph in cm
-i set heIght of graph in cm
-v Verbose

EOF
}

finish() {
rm -f $IN $OUT ${OUT%.png} ${ERR%.err}
rm -f $IN $OUT ${OUT%.png} ${OUT%.pdf} ${ERR%.err}

## Removes error file if error file is empty.
if [[ ! -s $ERR ]]; then
Expand All @@ -49,14 +52,16 @@ finish() {
trap finish EXIT

callR() {
Rscript --vanilla -e "options(scipen=999);df<-read.csv('${IN}',header=${HEADER},sep='${DELIMITER}',stringsAsFactors=F);${REQUIRES}${SCRIPT}last<-.Last.value;if(is.matrix(last)){last<-as.data.frame(last)};if(is.data.frame(last)){write.table(last,'${OUT}',sep=',',quote=T,qmethod='double',row.names=F,col.names=${HEADER});}else if(is.vector(last)){cat(last,sep='\\\n', file='${OUT}')}else if(exists('is.ggplot')&&is.ggplot(last)){ggsave('${OUT}',last,dpi=${RIO_DPI-72},units='cm',width=20,height=15);}else{sink('${OUT}');print(last);}"
Rscript --vanilla -e "options(scipen=999);df<-read.csv('${IN}',header=${HEADER},sep='${DELIMITER}',stringsAsFactors=F);${REQUIRES}${SCRIPT}last<-.Last.value;if(is.matrix(last)){last<-as.data.frame(last)};if(is.data.frame(last)){write.table(last,'${OUT}',sep=',',quote=T,qmethod='double',row.names=F,col.names=${HEADER});}else if(is.vector(last)){cat(last,sep='\\\n', file='${OUT}')}else if(exists('is.ggplot')&&is.ggplot(last)){ggsave('${OUT}',last,dpi=${RIO_DPI-72},units='cm',width=${WIDTH},height=${HEIGHT});}else{sink('${OUT}');print(last);}"
}

SCRIPT=
REQUIRES=
DELIMITER=","
HEADER="T"
VERBOSE=false
WIDTH=20
HEIGHT=15

# OSX `mktemp' requires a temp file template, but Linux `mktemp' has it as optional.
# This explicitly uses a template, which works for both. The $TMPDIR is in case
Expand All @@ -68,7 +73,7 @@ IN=$(mktemp ${TMPDIR}/Rio-XXXXXXXX)
OUT=$(mktemp ${TMPDIR}/Rio-XXXXXXXX).png
ERR=$(mktemp ${TMPDIR}/Rio-XXXXXXXX).err

while getopts "d:hgnprsve:f:b" OPTION
while getopts "d:hgnprspve:f:bw:i:" OPTION
do
case $OPTION in
b)
Expand Down Expand Up @@ -106,6 +111,15 @@ do
v)
VERBOSE=true
;;
p)
OUT=${OUT%.png}.pdf
;;
w)
WIDTH=$OPTARG
;;
i)
HEIGHT=$OPTARG
;;
?)
usage
exit
Expand Down