-1

Anyone got an idea why

system2("bash", "ls") 

would result in (both in R-Gui and RStudio Console)

/usr/bin/ls: /usr/bin/ls: cannot execute binary file 

while other shells work w/o issues (see example below) and the "bash" in the RStudio Terminal also works w/o problems. AND if there is a remedy for that?

Working system2 example:

system2("powershell", "dir") 

Running R-3.6.3 on Windows 10 here ... with "Git bash" executing as "bash".

PS: just in case someone wonders - I also tried the full path ie /usr/bin/ls -> same result

GWD
  • 1,387
  • 10
  • 22
  • `ls` is the plain vanilla "list content of current directory" bash command; just as "dir" is for powershell – GWD Feb 05 '21 at 13:40
  • 1
    Okay, but `bash ls` runs `ls` as a shell script, you need to do `bash -c ls`. – oguz ismail Feb 05 '21 at 13:46
  • @oguzismail `args = "-c ls"` also works ... – GWD Feb 05 '21 at 13:49
  • @oguzismail - I was also always under the impression that `system2` would do exactly that `-c` part under the hood - as it does with `powershell` and `dir` (as its `args`) BUT never mind. Really fascinated by the downvotes especially since `system2` jumps all over the place depending on what combination of R, RStudio, PATH and Locale you are running. I thought that to be quite interesting to keep as a note to self - so glad to have that in here as my reminder. – GWD Feb 05 '21 at 14:43
  • 1
    I don't know R at all, I saw this question because you tagged it bash. – oguz ismail Feb 05 '21 at 14:45

1 Answers1

1

Ok - this does the trick

system2("bash", input = "ls")

so instead of args = ... as with the powershell command one needs (or more interestingly sometimes 'can') use the input = ... parameter with bash

OR as mentioned in the comments by @oguzismail this will also work

system2("bash", "-c ls") 

as well as pointed out by @Christoph

system2("ls") 

also works sometimes (ie in some RStudio[?] setups it will not return results in some it will).

But I have come to notice that different combinations of R-versions with different RStudio versions [or more probably Locales] will not always behave consistently of course also depending on the availability of bash commands like ls (more generally /usr/bin) being on the PATH or not.

So choose what suits u best!

GWD
  • 1,387
  • 10
  • 22
  • 3
    Why not just `system2("ls")`? – Christoph Feb 05 '21 at 13:59
  • @Christoph - never expected that to work as well under WINDOWS, BUT after checking it seems to do the trick as well in SOME configurations BUT NOT ALL! Guess depends on PATH variable as well as some combinations of R and RStudio sometimes accepting parameters and sometimes rejecting them (eg because of inserting `\r` etc). It all looks a bit shaky underneath the ground ... – GWD Feb 05 '21 at 14:35