Is it possible to bypass the 9-argument limit in `commandArgs()` for R programs in a bash script?

71 views Asked by At

i'm currently running a r program where i use already 9 argument with "commandArgs()" for my bash script, but it seems i can't use a 10th argument, the program doesn't take it, is there a way i could do it and won't have to make an other program just for that ? thanks in advance for your help ! After some question here is the script:

library(tidyverse)
library(readr)
library(DT)Args 
library(data.table)

args <- commandArgs()

table1 <- read.csv(args[6])
table2 <- read.csv(args[7])

idem <-as.character(intersect(table1$x, table2$x))
same <- data.frame( nom_colonne = idem)

pasidem <-as.character(setdiff(table1$x, table2$x))
passame <- data.frame( nom_colonne = pasidem)

pasidem2 <-as.character(setdiff(table2$x, table1$x))
notsame <- data.frame( nom_colonne = pasidem2)

write.csv(same, args[8], row.names = F,  quote=F)
write.csv(passame, args[9], row.names = F,  quote=F)
write.csv(passame, args[10], row.names = F,  quote=F)

As you can see i ask args 6 to 10 (since 1 to 5 already taken by the system) but it seems i can't use a 10th argument

2

There are 2 answers

2
chandra On

BASH has a builtin named shift which might help you.

#!/usr/bin/bash

echo ${1} ${9}
shift 9
echo ${1}

If you save the above script as shift_example.bash and make it executable then you can call it as below to see what I mean.

./shift_example.bash A B C D E F G H I J K L

The second echo should print J which is the tenth argument.

See https://www.gnu.org/software/bash/manual/bash.html for details.

0
Dirk is no longer here On

You can use littler and its r front-end. It provides a vector argv (just like C does) which is also easier to use programmatically than commandArgs(). (We most often use this with additional command-line parsing packages; I liked docopt a lot and have many examples.)

Code

#!/usr/bin/r

for (i in seq_along(argv)) {
    cat("Argument", i, "is", argv[i], "\n")
}

Demo

$ ./argcount.r a b c d e f g h i j k l
Argument 1 is a 
Argument 2 is b 
Argument 3 is c 
Argument 4 is d 
Argument 5 is e 
Argument 6 is f 
Argument 7 is g 
Argument 8 is h 
Argument 9 is i 
Argument 10 is j 
Argument 11 is k 
Argument 12 is l 
$ 

(This worked all the way up to 26 when I just tested with all letters.)

Littler ships with a Makevars for Linux and macOS and could build on Windows but I have not a need. If someone wants to "port" this, the very similar package RInside is structured similarly and can provide a model.