How to pass command line arguments to run a Rscript

0 votes

Hello

I am a new user of R and I am stuck. I want to execute a Rscript that will allow me to compute some indices for many stations once. Here is what the manual says to do:

5.2 Calculating the indices on multiple station files 

The script that provides this functionality is climpact2.batch.stations.r. This script requires command line arguments to be passed to it at run time. Execution of this script takes the following form, from the Linux command line:

         Rscript climpact2.batch.stations.r /full/path/to/station/files/ /full/path/to/metadata.txt base_period_begin base_period_end cores_to_use

The 5 command line arguments above are defined in the following table.

Table 2. Command line arguments to pass to climpact2.batch.stations.r

/full/path/to/station/files/

Directory where individual station files are kept. An example can be found in sample_data/XXXX

/full/path/to/metadata.txt

Text file that contains information about each station file to process.

base_period_begin

Beginning year for the base period. To be used on all stations.

base_period_end

Ending year for the base period. To be used on all stations.

cores_to_use

Number of processor cores to use. When processing hundreds or thousands of files, this is useful.

An example of executing the climpact2.batch.stations.r file would be:

        Rscript climpact2.batch.stations.r ./sample_data/Renovados_hasta_2010 ./sample_data/climpact2.sample.batch.metadata.txt 1971 2000 4

I don't know how to run my script in R to get results. Please help me
 

Aug 4, 2018 in Data Analytics by Wendso
• 120 points
3,756 views

1 answer to this question.

0 votes

1. For taking an argument from the command line you'll have to create a function and save in an R file example as shown below:

add <- function(x+y) {
    x+y
}
Save it as a R file say "add.R"
2. Next in the command line or R script call the R script as shown in the function below:
From R GUI's command line:
source("add.R")
add(3, 5)
From R Script:
args <- commandArgs(TRUE)
x <- as.integer(args[1])
y <- as.integer(args[2])

source("add.R")

add(x,y)



answered Aug 6, 2018 by Anmol
• 1,780 points

Should it not have been

add <- function(x, y) {
    x+y
}

instead of:

add <- function(x+y) {
    x+y
}

as you call add(3, 5) in the main R script?

Related Questions In Data Analytics

0 votes
1 answer

how to run a logistic regression with clustered standard errors in R?

have a look at rms package. lrm is logistic ...READ MORE

answered Nov 6, 2018 in Data Analytics by Kalgi
• 52,290 points
519 views
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,650 points
302 views
0 votes
1 answer

How to run a file with .r extension?

Follow the below command: C:\Program Files\R\R-3.1.1\bin\Rscript.e ...READ MORE

answered Apr 29, 2019 in Data Analytics by Tyrion anex
• 8,650 points
228 views
0 votes
1 answer

How to create a line chart in R?

Hi@akhtar, A simple line chart is created using ...READ MORE

answered Oct 29, 2020 in Data Analytics by MD
• 95,060 points
45 views
0 votes
3 answers

How can I add YAML current date in rmarkdown ?

<$today:MM/dd/yyyy> READ MORE

answered Mar 25, 2019 in Data Analytics by Anupam Das
6,246 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 8, 2018 in Data Analytics by Abhi
• 3,720 points
270 views
+1 vote
4 answers

Python vs. R for data science

I would say both Python and R ...READ MORE

answered Aug 1, 2019 in Data Analytics by briny
388 views
0 votes
1 answer

Why is data cleaning needed?

Data cleaning is the fourth step in ...READ MORE

answered Nov 14, 2018 in Data Analytics by Maverick
• 10,840 points
140 views
+1 vote
2 answers

avoid producing a eps-graphic with 2 pages by mixing traditional and grid graphics

The bv_set() is missing from your code hence ...READ MORE

answered Aug 27, 2018 in Data Analytics by Anmol
• 1,780 points
130 views
0 votes
2 answers

When scoring a logistic regression model , is having the predicted variable in test dataset mandatory ?

Answer to your follow up question: We can ...READ MORE

answered Oct 17, 2018 in Data Analytics by Anmol
• 1,780 points
230 views