1

As best as I can sort out (since I haven't had much luck finding documentation of it) when one runs Rscript with a command argument that includes a wildcard *, the argument is expanded to a character vector of the filepaths that match, or passed if there are no matches. Is there a way to pass the wildcard all the time, so I can handle it myself within the script (using things like Sys.glob, for example)?

Here's a minimal example, run from the terminal:

ls
## foo.csv bar.csv baz.txt
Rscript -e "print(commandArgs(T))" *.csv
## [1] "foo.csv" "bar.csv"
Rscript -e "print(commandArgs(T))" *.txt
## [1] "baz.txt"
Rscript -e "print(commandArgs(T))" *.rds
## [1] "*.rds"

EDIT: I have learned that this behavior is from bash, not Rscript. Is there some way to work around this behavior from within R, or to suppress wildcard expansion for a particular R script but not the Rscript command? In my particular case, I want to run a function with two arguments, Rscript collapse.R *.rds out.rds that concatenates the contents of many individual RDS files into a list and saves the result in out.rds. But since the wildcard gets expanded before being passed to R, I have no way of checking whether the second argument has been supplied.

Empiromancer
  • 3,778
  • 1
  • 22
  • 53
  • I assume you're on *nix, as Windows cmd doesn't do expansion. See http://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion – Hong Ooi Nov 02 '16 at 01:46
  • Post some examples ... plz. – IRTFM Nov 02 '16 at 05:57
  • @42- Thanks! Added an example. – Empiromancer Nov 02 '16 at 17:15
  • @HongOoi Helpful link. Is there any way of working around that behavior within R - collapsing those arguments back into a single string somehow? I don't want to turn off globbing for the Rscript command in general, just this one program - and I'm not sure I have the approriate permissions to muck around with the config files on the computer I'm working on. – Empiromancer Nov 02 '16 at 17:20

1 Answers1

2

If I understand correctly, you don't want bash to glob the wildcard for you, you want to pass the expression itself, e.g. *.csv. Some options include:

  1. Pass the expression in quoted text and process it within R, either by evaluating that in another command or otherwise

    Rscript -e "list.files(pattern = commandArgs(T))" "*\.csv$"
    
  2. Pass just the extension and process the * within R by context

    Rscript -e "list.files(pattern = paste0('*\\\\.', commandArgs(T)))" "csv$"
    
  3. Through complicated and unnecessary means, disable globbing for that command: Stop shell wildcard character expansion?

Note: I've changed the argument to a regex to prevent it matching too greedily.

Community
  • 1
  • 1
Jonathan Carroll
  • 3,897
  • 14
  • 34