Reputation: 463
I work on a data.frame (1000 rows and 3 columns) . I use an argument on the 3rd column (which corresponds to correlations) to select values upper or lower than my argument.
Df<-get(load("test.RData"))
library(optparse)
args <- commandArgs(TRUE)
#get options
option_list = list(
make_option(c("-t", "--threshold"), type="double", default=NULL));
opt_parser= OptionParser(usage = "Usage: %prog -f [FILE]",option_list=option_list, description= "Description:")
opt = parse_args(opt_parser)
library(dplyr)
Df=Df%>%filter(corr>opt$threshold)
save(Df, file="corr.Rda")
Then, I would like to use Slurm to run this code.
test.sh
#!/bin/bash
#SBATCH -o job-%A_%a_task.out
#SBATCH --job-name=cor
#SBATCH --partition=normal
#SBATCH --time=1-00:00:00
#SBATCH --mem=1G
#SBATCH --cpus-per-task=2
#Set up whatever package we need to run with
module load gcc/8.1.0 openblas/0.3.3 R
export FILENAME=~/test.R
Rscript $FILENAME --threshold $1
My question is this one : do I neccesarily need to put an argument on the sbatch command line? For example, if I run sbatch test.sh 0.7
, it will work and I will get the correlations > 0.7. But if I don't want to put an argument, in the goal to get all correlations, I will run ``sbatch test.sh``` , I get
Error in getopt(spec = spec, opt = args) :
flag "threshold" requires an argument
Edit : if I run sbatch test.sh -1
, I will get all the correlations , but I just would like to know if it possible to don't put any argument and get all the correlations?
Any idea?
Upvotes: 0
Views: 511
Reputation: 5762
You are using
Rscript $FILENAME --threshold $1
When you give a parameter like 0.7
, it gets substituted by
Rscript $FILENAME --threshold 0.7
But when you give no parameter, then you get:
Rscript $FILENAME --threshold
And as the message says, --threshold
requires an argument.
You can test $1
for its existence and only pass the --threshold
argument when required:
threshold_args=()
if [ -n "$1" ]
then
threshold_args+=("--threshold" "$1")
fi
Rscript $FILENAME "${threshold_args[@]}"
Upvotes: 1