Data from hive to R to bash - r

Im trying to execute hive query in R using system command. This R function is called from a Bash script.
R Code
hivedata<-function(query)
{
data <- system(paste0("hive -S -e ", query), wait = TRUE,intern=TRUE)
if (identical(data, character(0))){data=NULL}
message("return value:")
message(data)
message("return value type:")
message(class(data))
return(cat(data))
}
if (length(query)>0 && is.na(query)==FALSE){
data=hivedata(query)
print(data)
}
Bash function
gethivedata(){
set -f #disable aterisk in sql getting expanded as filenames
query=$1
data=`Rscript hivedata.r "'$query'"`
echo $data
}
Calling function in Bash
totalcount=$(gethivedata " select count(*) from hivedb.hivetable ")
The outputs
[usr#host dir]$ totalcount=$(execute_sql " select count(*) from
hivedb.hivetable ")
return value:
0
return value type:
character
-------------------------------
[usr#host dir]$ echo $totalcount
0NULL
When cat is not used, the output value comes as [1]"0". Because R returns the index also with the output. When cat is used then the output becomes 0NULL. I want only the actual value which is "0"
What does [1] mean in the output of any command executed on R command line?

Related

How to call exe program and input parameters using R?

I want to call .exe program (spi_sl_6.exe) using a command of R (system), however I can't input parameters to the program using "system". The followwing is my command and parameters:system("D:\\working\spi_sl_6.exe")
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Thanks in advance.
This is using the Standardized Precipitation Index software from
http://drought.unl.edu/MonitoringTools/DownloadableSPIProgram.aspx.
This seems to give a working solution using Windows (but not without warnings!)
Fisrt download the software and example files
# Create directory to download software
mydir <- "C:\\Users\\david\\spi"
dir.create(mydir)
url <- "http://drought.unl.edu/archive/Programs/SPI"
download.file(file.path(url, "spi_sl_6.exe"), file.path(mydir, "spi_sl_6.exe"), mode="wb")
# Download example files
download.file(file.path(url, "SPI_samplefiles.zip"), file.path(mydir, "SPI_samplefiles.zip"))
# extract one example file, and write out
temp <- unzip(file.path(mydir, "SPI_samplefiles.zip"), "wymo.cor")
dat <- read.table(temp)
# Use this file as an example input
write.table(dat, file.path(mydir,"wymo.cor"), col.names = FALSE, row.names = FALSE)
From page 3 of the help file basic-spi-program-information.pdf at the above link the command line code should be of the form spi 3 6 12 <infile.dat >outfile.dat, however,
neither of the following worked (just from command line not in R), and various iterations of how to pass parameters.
C:\Users\david\spi\spi_sl_6 3 <C:\Users\david\spi\wymo.cor >C:\Users\david\spi\out.dat
cd C:\Users\david\spi && spi_sl_6 3 <wymo.cor >out.dat
However, using the accepted answer from Running .exe file with multiple parameters in c#
seems to work. That is again from the command line
cd C:\Users\david\spi && (echo 2 && echo 3 && echo 6 && echo wymo.cor && echo out1.dat) | spi_sl_6
So to run this in R you can wrap this in a shell (you will need to change the path to where you have saved the exe)
shell("cd C:\\Users\\david\\spi && (echo 2 && echo 3 && echo 6 && echo wymo.cor && echo out2.dat) | spi_sl_6", intern=TRUE)
out1.dat and out2.dat should be the same.
This throws warning messages, I think from the echo (in R but not from command line) but the output file is produced.
Suppose you can automate all the echo calls sligtly, so all you need to do is input the time parameters.
timez <- c(2, 3, 6)
stime <- paste("echo", timez, collapse =" && ")
infile <- "wymo.cor"
outfile <- "out3.dat"
spiCall <- paste("cd", mydir, "&& (" , stime, "&& echo", infile, "&& echo", outfile, " ) | spi_sl_6")
shell(spiCall)
You can construct the command using sprintf :
cmd_name <- "D:\\working\spi_sl_6.exe"
param1 <- "a"
param2 <- "b"
system2(sprintf("%s %s %s",cmd_name,param1,param2))
Or using system2( I prefer this option):
system2(cmd_name, args = c(param1,param2))

Returning an argument from R to bash

I have the following bash script in which an R script is called
#!/bin/bash
declare -x a=33
declare -x b=1
declare -x c=0
Rscript --vanilla MWE.R $a $b $c
echo $a $b $c
I want to modify the bash variables in the R script and return their modified values in the bash script because I am then passing the modified variables somewhere else. The R script is
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
Rb = as.numeric(args[2])
Rc = as.numeric(args[3])
Rb = Rb + 1
Rc = Rc + 1
args[2]=Rb
args[3]=Rc
print(c(args[1],args[2],args[3]))
However, the output of the print and echo respectively are:
[1] "33" "2" "1"
33 1 0
which shows that the new values aren't passed from R to bash. What am I doing wrong?
As Rscript does not allow environment variable manipulation you will need to capture the R output from the bash program.
One of the many possibilities is to use an array:
#!/bin/bash
declare a=33
declare b=1
declare c=0
declare -a RESULT
RESULT=($(Rscript --vanilla MWE.R $a $b $c))
a=${RESULT[1]}
b=${RESULT[2]}
c=${RESULT[3]}

R: pass variable from R to unix

I am running an R script via bash script and want to return the output of the R script to the bash script to keep working with it there.
The bash is sth like this:
#!/bin/bash
Rscript MYRScript.R
a=OUTPUT_FROM_MYRScript.R
do sth with a
and the R script is sth like this:
for(i in 1:5){
i
sink(type="message")
}
I want bash to work with one variable from R at the time, meaning: bash receives i=1 and works with that, when that task is done, receives i=2 and so on.
Any ideas how to do that?
One option is to make your R script executable with #!/usr/bin/env Rscript (setting the executable bit; e.g. chmod 0755 myrscript.r, chmod +x myrscript.r, etc...), and just treat it like any other command, e.g. assigning the results to an array variable below:
myrscript.r
#!/usr/bin/env Rscript
cat(1:5, sep = "\n")
mybashscript.sh
#!/bin/bash
RES=($(./myrscript.r))
for elem in "${RES[#]}"
do
echo elem is "${elem}"
done
nrussell$ ./mybashscript.sh
elem is 1
elem is 2
elem is 3
elem is 4
elem is 5
Here is MYRScript.R:
for(iter in 1:5) {
cat(iter, ' ')
}
and here is your bash script:
#!/bin/bash
r_output=`Rscript ~/MYRscript.R`
for iter in `echo $r_output`
do
echo Here is some output from R: $iter
done
Here is some output from R: 1
Here is some output from R: 2
Here is some output from R: 3
Here is some output from R: 4
Here is some output from R: 5

Assigning a value to a variable in a loop and printing it outside the loop using korn shell

I am having problem when trying to assign a value to a variable in a loop and trying to print it outside the loop using korn shell. I want to use that variable in later part of my script. So I am trying to test by printing the value of the dynamic variable. I just assigned to it from my array.
#!/usr/bin/ksh
clear
BINPATH=/usr/bin
SVR_LIST=servers_list
set -A SERVERS `cat $SVR_LIST`
typeset -i i=0
Sn=${#SERVERS[#]}
#echo "Number of servers in an array are .................." $Sn
while [ $i -lt ${#SERVERS[#]} ] ; do
#print ${SERVERS[$i]}
typeset -l s${i}=${SERVERS[$i]}
#eval echo "Value of Variable is" \${s$i}
#s=\${s$i}
(( i=i+1 ))
done
s=\${s$i}
eval echo "value of s is " $s
s=eval \${s0}
APPSERVER1=$s
echo $APPSERVER1
s=eval \${s1}
APPSERVER2=$s
echo $APPSERVER2
I am getting following error.
value of s is
./variableTest.sh[21]: ${s0}:not found
${s4}
./variableTest.sh[24]: ${s1}:not found
${s4}
here is working codeā€¦
#!/bin/ksh
clear
#BINPATH=/usr/bin
SVR_LIST=test
set -A SERVERS `cat $SVR_LIST`
typeset -i i=0
Sn=${#SERVERS[#]}
#echo "Number of servers in an array are .................." $Sn
while [ $i -lt ${#SERVERS[#]} ] ; do
#print ${SERVERS[$i]}
typeset -l s${i}=${SERVERS[$i]}
eval echo "Value of Variable is" \${s$i}
#s=\${s$i}
(( i=i+1 ))
done
#NOTE THIS LINE i value is i+1 here..
#so if you had last variable as s10=abc you are using s11 outside the loop in follwoing two lines..now its s10
let i=i-1
s=\${s$i}
eval echo "value of s is " $s
#CHANGES HERE
s=$s0
APPSERVER1=$s
echo $APPSERVER1
s=$s1
APPSERVER2=$s
echo $APPSERVER2
output...
Value of Variable is "credit":
Value of Variable is "wic":
Value of Variable is "wiccash":
Value of Variable is "sfmnp":
Value of Variable is "snap":
Value of Variable is "baked goods":
Value of Variable is "baked goods":
"credit":
"wic":

two layers of quotes around a bash variable

I've written a bash script that makes a series of R scripts. However, Ive run into difficulty quoting a bash variable to echo to the R script as a file to be read into R. I have
echo "loadings_file <- $loadings ; calls_file <- $file" | cat - template.R > temp && mv temp $scriptname
$loadings and $file are files I want R to read in. But when I run it as is they end up in the R script with no quotes aroudn them for R to treat as a string. How do I make sure they're quoted in R but still expanded in bash first?
echo "loadings_file <- '$loadings' ; calls_file <- '$file'"
If you specifically need double quoting:
echo "loadings_file <- \"$loadings\" ; calls_file <- \"$file\""
You have to escape your quotes (\") around the variables:
echo "loadings_file <- \"$loadings\" ; calls_file <- \"$file\"" | cat - template.R > temp && mv temp $scriptname

Resources