0

I checked How to execute git-bash command with system() or shell() in R but this didn't exactly solve the problem for me. I'm very new to R, I'm using it on Windows and I'm modifying another project. I suspect originally this project was written for a different OS.

At one point, the main script calls a .sh file from inside R in a for loop, and uses system() to run it. The sh file creates a new directory, copies files from one directory to the other and modifies them slightly (removes first row & adds another).

The part in the code that calls the file goes like this: (run_this is the sh file we want to run)

directory_name = "data/clean"
for (i in 1:n) {
    filename = sprintf("%s.json",i)
    cmd = sprintf("run_this.sh %s %s", filename, directory_name)
    system(cmd)
}

I suspect system() calls command prompt in Windows, which I've checked doesn't run this sh file. But I've found I can run them from Git Bash. Unfortunately I'd have to do them one by one if I choose this option and since n is large this doesn't work very well for me.

So, I'm wondering 1) is there any way to direct system() or system2() to use Git Bash from inside R? (I have Git Bash to my environment variables.) 2) any other possible solutions to run sh files from command prompt?

ladyp
  • 25
  • 5
  • Do this work on the terminal? I would suggest that you first can try this on the terminal and then try to do it through R. If you don't use `bash /path/to/yourscript.sh` then you have to make the file executable with `chxmod -x`. As an alternative to system, you can also try `processx::run`. – Kostas Vasilopoulos Jun 08 '20 at 14:08
  • @KostasVasilopoulos It doesn't work from terminal - they've checked already. OP says *"I suspect system() calls command prompt in Windows, which I've checked doesn't run this sh file."* – Gregor Thomas Jun 08 '20 at 14:15
  • Possible duplicate: [How to run .sh on Windows Command Prompt](https://stackoverflow.com/q/26522789/903061), at least some of the answers there should help. – Gregor Thomas Jun 08 '20 at 14:17
  • Alternately, if the loop really is that simple, rewriting the loop in bash would be pretty easy too. Then you could loop using your git bash. – Gregor Thomas Jun 08 '20 at 14:25
  • Hi both, thanks! I was able to run it in command prompt (after slightly changing the sh file, there was something following a command (mkdir) that didn't make sense in windows and that was causing the issue) but it still doesn't run from R. I'll look into writing a loop for Git Bash, but do you know why it wouldn't run from inside R? – ladyp Jun 08 '20 at 14:46
  • If you control `run_this.sh`, perhaps it would be better to write the script so that it accepts the target directory as its first argument, and then the second *and following* (no limit) arguments are taken as files, effectively vectorizing your script. If `directory_name` is optional, then perhaps use bash's `getopts` so that you could say `run_this.sh -d tgtdir file1 ...`. Internally, `TGTDIR=$1 ; shift 1 ; for FN in ${@}; do ... ; done` in the bash script. – r2evans Jun 08 '20 at 16:52
  • (Doing it that way is analogous to R's `...` function argument.) – r2evans Jun 08 '20 at 16:52
  • thanks @r2evans! I ended up doing something very similar (for bash). – ladyp Jun 09 '20 at 13:30

0 Answers0