jakob r jakob r - 2 months ago 7
Bash Question

Call shell skript with specific working directory from R

I have an sh script, that can only be run from a specific directory because it looks for the files in that directory and I don't want to change the sh file.

Suppose the file is in

./sub/script.sh
and the R working directory is
./


What works is

wd = getwd()
setwd("./sub")
system2("./script.sh")
setwd(wd)


But I find that rather unhandy. How can I do without changing the R working direcotry?

Answer

As system2 uses shQuote we can only use system() and as already pointed out by @Dirk you can then use

system("cd ./sub && sh script.sh")