Running R file using sh/bash - r

I have created below .sh file to run R code saved in separate .R file.
cat EE.sh
#!/bin/bash
VARIABLES=( 20190719 20190718 )
for i in ${VARIABLES[#]}; do
VARIABLENAME=$i
/usr/lib/R/bin/Rscript -e 'source("/home/EER.R")'
Basically what it is expected to do is, take the dates from VARIABLE and pass to the /home/EER.R file, and R will do execution based on passed date (after correct formatting)
Then I ran below code
sudo chmod a+rx EE.sh
and
sudo bash EE.sh
But I then get below error message.
sudo bash EE.sh
EE.sh: line 2: $'\r': command not found
EE.sh: line 3: $'\r': command not found
EE.sh: line 4: $'\r': command not found
Can anyone help me to resolve this issue.
I am using Ubuntu 18 with R version 3.4.4 (2018-03-15)

This problem looks to be related to carriage returns related(which come when we copy text from windows machine to unix machine), so to identify them use:
cat -v Input_file
If you see carriage returns in your file then try:
tr -d '\r' < Input_file > temp && mv temp Input_file
Once they are removed then try to run your program.

Related

How to execute raster2pgsql with R?

I'm trying to execute the following code in R
system('"C:/Program Files/PostgreSQL/9.5/bin/raster2pgsql" -s 32630 -a -f raster Y:/Sen2R_Download/prueba_sergio/raster3/SCL/S2B2A_20180731_137_sen2r_SCL_10.tif sentinel > Y:/Sen2R_Download/prueba_sergio/rastersql27.sql')
But it throws an error
ERROR: Unable to read raster file: sentinel
But this error shouldn't happen and when I execute the same in cmd it works well
C:\Users\Public\Documents>"C:/Program Files/PostgreSQL/9.5/bin/raster2pgsql" -s 32630 -a -f raster Y:/Sen2R_Download/prueba_sergio/raster3/SCL/S2B2A_20180731_137_sen2r_SCL_10.tif sentinel > Y:/Sen2R_Download/prueba_sergio/rastersql27.sql
Processing 1/1: Y:/Sen2R_Download/prueba_sergio/raster3/SCL/S2B2A_20180731_137_sen2r_SCL_10.tif
What can I do to make it work with R?
Try running the same command with system2 instead.
See https://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html for more details. Specifically the section regarding differences between Unix and windows

UNIX commands from R via shell function

I need to issue unix commands from an R session. I'm on Windows R2 2012 server using RStudio 1.1.383 and R 3.4.3.
The shell() function looks to be the right one for me but when I specify the path to my bash shell (from Git for Windows install) the command fails with error code 127.
shell_path <- "C:\\Program Files\\Git\\git-bash.exe"
shell("ls -a", shell = shell_path)
## running command 'C:\Program Files\Git\git-bash.exe /c ls -a' had status 127'ls -a' execution failed with error code 127
Pretty sure my shell path is correct:
What am I doing wrong?
EDIT: for clarity I would like to pass any number of UNIX commands, I am just using ls -a for an example.
EDIT:
After some playing about 2018-03-09:
shell(cmd = "ls -a", shell = '"C:/Program Files/Git/bin/bash.exe"', intern = TRUE, flag = "-c")
The correct location of my bash.exe was at .../bin/bash.exe. This uses shell with intern = TRUE to return the output as an R object. Note the use of single quote marks around the shell path.
EDIT: 2018-03-09 21:40:46 UT
In RStudio we can also call bash using knitr and setting chunk options:
library(knitr)
```{bash my_bash_chunk, engine.path="C:\\Program Files\\Git\\bin\\bash.exe"}
# Using a call to unix shell
ls -a
```
Two things stand out here. Bash will return exit code 127 if a command is not found; you should try running the fully qualified command name.
I also see that your shell is being run with a /c flag. According to the documentation, the flag argument specifies "the switch to run a command under the shell" and it defaults to /c, but "if the shell is bash or tcsh or sh the default is changed to '-c'." Obviously this isn't happening for git-bash.exe.
Try these changes out:
shell_path <- "C:\\Program Files\\Git\\git-bash.exe"
shell("/bin/ls -a", shell = shell_path, flag = "-c")
Not on Windows, so can't be sure this will work.
Perhaps you need to use shQuote?
shell( paste("ls -a ", shQuote( shell_path) ) )
(Untested. I'm not on Windows. But do read ?shQuote))
If you just want to do ls -a, you can use the below commands:
shell("'ls -a'", shell="C:\\Git\\bin\\sh.exe")
#or
shell('C:\\Git\\bin\\sh.exe -c "ls -a"')
Let us know if the space in "Program Files" is causing problems.
And if you require login before you can call your command,
shell('C:\\Git\\bin\\sh.exe --login -c "ls -a"')
But if you are looking at performing git commands from R, the git2r by ropensci might suit your needs.

How to get latest RStudio version

I am struggling with writing a script that would somehow scrape the https://www.rstudio.com/products/rstudio/download/ for the number of the latest RStudio version, download it and install it.
Since I am an R programmer, I started to write an R script using rvest package. I managed to scrape the download link for the RStudio server, but I still cannot get the RStudio itself.
Here is the R code for getting a download link for the 64bit RStudio-server for Ubuntu.
if(!require('stringr')) install.packages('stringr', Ncpus=8, repos='http://cran.us.r-project.org')
if(!require('rvest')) install.packages('rvest', Ncpus=8, repos='http://cran.us.r-project.org')
xpath<-'//code[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]'
url<-'https://www.rstudio.com/products/rstudio/download-server/'
thepage<-xml2::read_html(url)
the_links_html <- rvest::html_nodes(thepage,xpath=xpath)
the_links <- rvest::html_text(the_links_html)
the_link <- the_links[stringr::str_detect(the_links, '-amd64\\\\.deb')]
the_r_uri<-stringr::str_match(the_link, 'https://.*$')
cat(the_r_uri)
Unfortunately, the RStudio desktop download page has completely different layout, and I the same approach doesn't work here.
Can someone help me with this? I can't believe, that all the data scientist in the world manually upgrade their RStudio!
There is an even simpler version of the script, that reads the version of the RStudio-server. Bash version:
RSTUDIO_LATEST=$(wget --no-check-certificate -qO- https://s3.amazonaws.com/rstudio-server/current.ver)
or R version:
scan('https://s3.amazonaws.com/rstudio-server/current.ver', what = character(0))
But the version of the RStudio-desktop still eludes me.
It seems that you can get the latest stable version number from the url http://download1.rstudio.org/current.ver and it is more up to date (for some unknown reason), at least at the time of writing this answer.
$ curl -s http://download1.rstudio.org/current.ver
1.1.447
$ curl -s https://www.rstudio.org/links/check_for_update?version=1.0.0 | grep -oEi 'update-version=([0-9]+\.[0-9]+\.[0-9]+)' | awk -F= '{print $2}'
1.1.423
Found that here: https://github.com/yutannihilation/ansible-playbook-r/blob/master/tasks/install-rstudio-server.yml
If you query RStudio's check_for_update with a version string you'll get back the update version and the URL of where to get it from:
https://www.rstudio.org/links/check_for_update?version=1.0.0
update-version=1.0.153&update-url=https%3A%2F%2Fwww.rstudio.com%2Fproducts%2Frstudio%2Fdownload%2F&update-message=RStudio%201.0.153%20is%20now%20available%20%28you%27re%20using%201.0.0%29&update-urgent=0
See here:
https://github.com/rstudio/rstudio/blob/54cd3abcfc58837b433464c793fe9b03a87f0bb4/src/cpp/session/modules/SessionUpdates.R
If you really want to scrape it from the download page then I'd get the href of the <a> in the first <td> of the first <table> of class "downloads", and then parse out the three dot-separated numbers between "RStudio-" and ".exe". RStudio release versions over all platforms so getting it from the Windows download should be sufficient.
> url = "https://www.rstudio.com/products/rstudio/download/"
> thepage<-xml2::read_html(url)
> html_node(thepage, ".downloads td a") %>% html_attr("href")
[1] "https://download1.rstudio.org/RStudio-1.0.153.exe"
There's a nearly-solution here:
https://hub.docker.com/r/rocker/rstudio-daily/~/dockerfile/
In this script, which scrapes for the latest builds:
https://raw.githubusercontent.com/rocker-org/rstudio-daily/master/latest.R
You'll want to modify that script to be more strict about what it accepts, i.e. I would want this one rstudio-server-1.1.355-amd64.deb and not the stretch variant.
(But you can modify it to target the kind of build you want anyway, this is the daily builds, RStudio Server for Ubuntu.)
If anyone is interested, here is my ultimate RServer-desktop-on-Ubuntu update script. It installs RStudio-desktop 64bit and then, if Fira Console font is available, applies a patch from https://github.com/tonsky/FiraCode/wiki/RStudio-instructions for the RStudio, so the ligatures start working.
#!/bin/bash
if dpkg -s rstudio >/dev/null 2>/dev/null; then
ver=$(apt show rstudio | grep Version)
pattern='^Version: ([0-9.]+)\s*$'
if [[ $ver =~ $pattern ]]; then
ourversion=${BASH_REMATCH[1]}
netversion=$(Rscript -e 'cat(stringr::str_match(scan("https://www.rstudio.org/links/check_for_update?version=1.0.0", what = character(0), quiet=TRUE), "^[^=]+=([^\\&]+)\\&.*")[[2]])')
if [[ $ourversion != $netversion ]]; then
RSTUDIO_URI=$(Rscript /tmp/get_rstudio_uri.R)
fi
tee /tmp/get_rstudio_uri.R <<EOF
if(!require('rvest')) install.packages('rvest', repos='http://cran.us.r-project.org')
xpath='.downloads:nth-child(2) tr:nth-child(5) a'
url = "https://www.rstudio.com/products/rstudio/download/"
thepage<-xml2::read_html(url)
cat(html_node(thepage, xpath) %>% html_attr("href"))
EOF
RSTUDIO_URI=$(Rscript /tmp/get_rstudio_uri.R)
wget -c --output-document /tmp/rstudio.deb $RSTUDIO_URI
sudo dpkg -i /tmp/rstudio.deb
rm /tmp/rstudio.deb
rm /tmp/get_rstudio_uri.R
if fc-list |grep -q FiraCode; then
if !grep -q "text-rendering:" /usr/lib/rstudio/www/index.htm; then
sudo sed -i '/<head>/a<style>*{text-rendering: optimizeLegibility;}<\/style>' /usr/lib/rstudio/www/index.htm
fi
fi
fi
fi

how to tail -f on multiple files with a script?

I am trying to tail multiple files in a ksh. I have the following script:
test.sh
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file>out.txt
echo "\n"
done
It is only reading the first file argument I provide to the script. Not reading the other files as the argument to the script.
When I do this:
./test.sh /var/adm/messages /var/adm/logs
it is only reading the /var/adm/messages not the logs. Any ideas what I might be doing wrong
You should use double ">>" syntax to redirect the stream at the end of your output file.
A simple ">" redirection will write the stream at the beginning of the file and consequently it will remove the previous content.
So try :
#!/bin/ksh
for file in "$#"
do
# show tails of each in background.
tail -f $file >> out.txt & # Don't forget to add the last character
done
EDIT : If you want to use multi tail it's not installed by default. On Debian or Ubuntu you can use apt-get install multi tail.

Whats the difference between running a shell script as ./script.sh and sh script.sh

I have a script that looks like this
#!/bin/bash
function something() {
echo "hello world!!"
}
something | tee logfile
I have set the execute permission on this file and when I try running the file like this
$./script.sh
it runs perfectly fine, but when I run it on the command line like this
$sh script.sh
It throws up an error. Why does this happen and what are the ways in which I can fix this.
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn't support some of the extra features of Bash).
You can fix it by invoking it as bash script.sh, or if it's your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it - rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that's already working ;)
If your shell is indeed dash and you want to modify the script to be compatible, https://wiki.ubuntu.com/DashAsBinSh has a helpful guide to the differences. In your sample it looks like you'd just have to remove the function keyword.
if your script is at your present working directory and you issue ./script.sh, the kernel will read the shebang (first line) and execute the shell interpreter that is defined. you can also call your script.sh by specifying the path of the interpreter eg
/bin/bash myscript.sh
/bin/sh myscript.sh
/bin/ksh myscript.sh etc
By the way, you can also put your shebang like this (if you don't want to specify full path)
#!/usr/bin/env sh
sh script.sh forces the script to be executed within the sh - shell.
while simply starting it from command line uses the shell-environemnt you're in.
Please post the error message for further answers.
Random though on what the error may be:
path specified in first line /bin/bash is wrong -- maybe bash is not installed?

Resources