How do I get in R the name of currently executed script when called via `r BATCH script file` - r

I call a script from a shell command line with
r CMD BATCH myscript.R
What do I have to do in myscript.R to get a character vector with the name of the script (i.e. "myscript.R")?
Note:
I found numerous questions on similar topics, but couldn't find anything that work for me. Particularly from question Determine path of the executing script I got the modified script snippet:
args <- commandArgs(trailingOnly = F)
scriptPath <- sub("^--file=", "", args[grep("^--file=", args)])
but scriptPath is always empty (probably due to the way I call the script via the BATCH command).

A simple example with commandArgs() works fine for me:
myscript.R:
commandArgs()[3]
in the terminal:
R CMD BATCH myscript.R
and then cat myscript.Rout:
commandArgs()[3]
[1] "myscript.R"

I believe if you use Rscript instead of R CMD BATCH it will work for you. I read the same post when I was trying to figure out how set working directory to directory of myscript. Anyway to run Rscript
"....\R-3.0.1\bin\x64\Rscript.exe" Myscript.r > Myscript.r.rout
And here is my code to set working directory to directory of script. I kept trying different alternatives to this then I realized you need to be using Rscript for the bulk of them to work.
args <- commandArgs(trailingOnly = F)
scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))
setwd(scriptPath)

Related

Passing arguments to an R script from command lines when the argument is a string

I want to pass arguments to an R script from command lines when the argument is a string
I know that if the argument is a numeric value, I can write something in the command line :
R CMD BATCH "--args CHR=1" rfile.R test.Rout
But I want to input a file name such as "file1.txt" in r command. If I put
R CMD BATCH "--args CHR=1 file="file1.txt" rfile.R test.Rout
It does not work. How can I fix it?
Here is a simple R script which would take string inputs:
args <- commandArgs(trailingOnly = TRUE)
cat(args, sep = "\n")
I save the file as "test.R" in my home directory. In the command line I can then use:
Rscript test.R "file.txt"
The " are optional if your string does not have whitespace. But this would be recognised as two inputs:
Rscript test.R file 1.txt
There is a nice little tutorial from which I took this here.

Call a function in R Script from a Batch file

I've a batch file that calls R script. It works fine. I need to know how Can I Call a function in R Script from that Batch file in windows? How to call this Function with parameters:
PNLCalcMultipleDatesClient("2010-10-03", "2010-10-05", "XYZ")
This command line works but it doesn't have Function call that is in R Script. Can you please help me to modify this command line in Windows and call above function ?
"\\C1PRDTLS01.axinc.com\Dev\RiskClient\inputCData\PNLCalculation\R\R-3.1.1\bin\R.exe" CMD BATCH --no-save --no-restore "\\C1PRDTLS01.axinc.com\Dev\RiskClient\inputCData\PNLCalculation\RScript\RadarPNLTimeseries.R"
Here is the R Script:
PNLCalcMultipleDatesClient("2010-10-03", "2010-10-05", "Dunavant")
PNLCalcMultipleDatesClient <- function(begindate, enddate, Client)
{
# Do some operation here....
.....
......
}
Here is an example. Here is the Rscript that i have, i tend to save them as txt itself.
## Rscript: Passing arguments to the Rscripts likes args[i]
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
print(1:args[1])
df=data.frame(1:args[1])
write.csv(df, args[2])
Then your batch file would look like this. Then you feed those arguments either directly to a cmd or create a batch file out of it.
echo off
Rscript rparam.txt 1000 out.csv
For your case, your Rscript(R_with_par.R) would be:
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
x1=args[1]
x2=args[2]
x3=args[3]
PNLCalcMultipleDatesClient <- function(begindate, enddate, Client)
{
# Do some operation here....
.....
......
}
PNLCalcMultipleDatesClient(as.Date(x1), as.Date(x2), as.character(x3))
And your CMD command would be:
Rscript R_with_par.R 2010-10-03 2010-10-05 Dunavant
You have to make sure that parameters that you pass are in format required by R. Give the path of the R script if you are not in the same directory. Also Rscript is far better choice than R CMD.
Create R Function:
squared <- function(number) {
sqred <- number^2
cat(sqred)
}
squared(as.integer(commandArgs(trailingOnly = TRUE)))
Run R script from command prompt: (Your path could be different)
C:R/bin/RScript.exe" "C:/Rscript/command_line.R" 100
Note: First argument is path of Rscript.exe, Second Argument is path of your R script and third argument is function argument.

Different results from Rscript and R CMD BATCH

I have an inconsistency issue which I cannot explain when running an R script. I am not able to produce a reproducible example because there is a whole set of files/functions called by the entry script.
Using Rscript or RStudio with R v3.1.2 I obtain the results I'm expecting, however when calling R CMD BATCH from bash my script does not produce identical output. From bash, R seems to read the command line arguments correctly and reports them from the script, BUT in my code only the Rscript and RStudio source methods seem to use the parameter correctly in my code.
The 2 command line calls are as follows:
Rscript ./script/forecast_category_script.R "category='razors'" "cores=4L"
R CMD BATCH --no-save "--args category='razors' cores=4L" ./script/forecast_category_script.R ~/data/output/out.out
Is there any obvious reason why these inconsistencies might be occurring? I'd prefer to use R CMD BATCH as it redirects output to a file and when I migrate my code to the university cluster as a batch job through the scheduler I'd like to be able to follow what it has done.
UPDATE: changing this line resolves it but why?
Previously I had the following line in there, basically so when I was testing I didn't keep reloading the huge dataset if it was already loaded in my RStudio environment:
if(!exists("spi")) spi = f_load.spi(category = category)
Replaced it with this:
spi = f_load.spi(category = category)
The underlying function f_load_spi remained the same however:
f_load.spi = function(spi = NULL, category = "razors" , n=NULL) {
# check if the data is pre-loaded
if (is.null(spi)) {
fil = paste0(pth.data.storage, "categories/", category, "/", category, ".sp_ss.interp.rds")
print(fil)
spi = readRDS(fil)
}
# subset to a specific set of items
if (!is.null(n)) {
fc.items = unique(spi$fc.item)
rnd = sample(1:length(fc.items), n)
spi = spi[fc.item %in% fc.items[rnd]]
}
spi
}
For some reason the category variable was not being passed through properly into the function and it was loading a different category (beer rather than razors) which was an enormous file and not suitable for testing.
This still doesn't explain why Rscript and R CMD BATCH behaved differently.
It is possible that one of them is loading up a previously saved workspace and using global variables. Have you checked whether it matters which directory you are in or if there are any .Rhistory files present? One way to ensure that you don't have any hidden variables is to clear the worspace at the beginning of each script. For example, rm(list=ls()) as the first line of your Rscript.
Also, you can pipe output to a file with an Rscript using sink().

Run two different R scripts at a time on windows

I have two R scripts. one is: source("D:/source_code_CCA.r") and another one is: source("D:/source_code_DL.r"). These two R scripts are completely independent. I want to run these two r scripts at a time on same R console on windows. How can I do this? Please help!
You could create a new script containing the following lines, or execute them via interactive console -- after making sure that the directory containing Rscript.exe is in your PATH:
shell(cmd = 'Rscript.exe D:/source_code_cca.r', wait=FALSE)
shell(cmd = 'Rscript.exe D:/source_code_DL.r', wait=FALSE)
(If it's not already there, the value to add to path could be C:\Program Files\R\R-3.1.2\bin or C:\Program Files\R\R-3.1.2\bin\x64)
Note that if you use cats or messages, those would be captured if you direct them to a text file, such as in:
shell(cmd = 'Rscript.exe D:/source_code_cca.r > out1.log', wait=FALSE)
shell(cmd = 'Rscript.exe D:/source_code_DL.r > out2.log', wait=FALSE)
Or if you specify intern=TRUE and assign the function's result to some variable:
res1 <- shell(cmd = 'Rscript.exe D:/source_code_cca.r', wait=FALSE, intern=TRUE)
res2 <- shell(cmd = 'Rscript.exe D:/source_code_DL.r', wait=FALSE, intern=TRUE)
I believe you're using Windows, the path tells me :-)
Open two different cmd.exe windows.
Use the R command to call one of the script in the first window, and one for the second. This can be done with some minor magic like: R.exe --no-environ --no-save > "out.txt"
Hope that helps. Alternatively, you can spawn different processed if you want to call them within the same shell.

How to use knitr from command line with Rscript and command line argument?

I have an R code my_code.R which takes in an argument a file test.txt. I can use:
Rscript -e my_code.R test.txt
and run the script, but i want to use stitch() from knitR to generate the report of the script in pdf/tex.
I have trolled around stack overflow and used following suggestions, but didn't get any results:
Rscript -e "library(knitr);knit('my_code.R "-args arg1=test.txt" ')"
Rscript -e "knitr::stitch('my_code.R "-args arg1=test.txt"')"
Here is another similar discussion on what i want (link), but with option for adding argument(s).
I do not see why this is not possible. Here is my_code.R:
commandArgs(TRUE)
And I simply run
Rscript -e "library(knitr); stitch('my_code.R')" --args foo bar whatever=blabla
I get the output
It seems you did not use double quotes correctly in your original attempt. It should be
Rscript -e "library(knitr); stitch('my_code.R')" --args arg1=test.txt
You can use knit2pdfand pass parameters to the report using its envir argument.
In other words , the solution is to create 2 seperates files an :
R script where you call knit2pdf like this : m_code.R
a markdown report file (.Rnw,.Rmd) : m_code_report.Rnw
m_code.R
This script contains all you R code. The idea, is to create an environment variable "params" , where you put all parameters need to be displayed/presented.
library(knitr)
library(markdown)
ff <- commandArgs(TRUE)[1]
params <- new.env()
e$ff <- ff
## here I pass all the parameters to the report
## I assume that the report and the code R in the same location
## the result pdf also will be in the same location , otherwise you can set
## paths as you like
knit2pdf('m_code_report.Rnw',envir=params) report
m_code_report.Rnw
The report display resulted using variables contained in the environment "params".
\documentclass{article}
\begin{document}
<<>>=
summary(ff)
#
\end{document}
Then you call the Rscript using Rscript for example like this:
Rscript m_code.R "cars"

Resources