1

I have a batch file test.bat on my windows desktop with the following content:

cd /d W:\r\dev\
"C:\Program Files\R\R-3.5.0\bin\i386\Rscript.exe"  scripts\some_function.R

When executed, the referenced R script some_function.R begins to run and outputs messages to the console to let me know where it is. However, it stops after reaching a certain point in the R code then starts over and continues to do so until the session auto-terminates.

I've inspected the R code for an indication of why the batch file would continue to return to the beginning and I've found none. The R file works fine when run directly (from within R studio, for example).

I'm well aware that naming the batch file with a command that appears in the file can be problematic, but that's not what is happening here. I've tried multiple names for the batch file but that doesn't seem to solve the problem.

Below is the beginning of the code in the referenced file some_function.R that keeps getting re-executed:

library(data.table)

dir <- gsub(x= getwd(), pattern = "(.:/r/)(.*)",replacement = "\\1")
envir <- gsub(x= getwd(), pattern = "(.:/r/)(\\w+)",replacement = "\\2")

cat(paste("directory = "),dir,"\n","environment = ",envir,"\n")


##############################################################
# Load System Parameters                                     #
##############################################################

cat("Loading system parameters ...","\n")

sys_param_file_path <- paste0(dir,"shared/files/system/sys_param.csv")
sys_params <- data.table(read.csv(sys_param_file_path, stringsAsFactors = FALSE))
sys_params <- sys_params[param_envir == envir,]

for(i in 1:nrow(sys_params)){
  if(sys_params[i,param_type] == "environment path"){
    sys_params[i,param_val := paste(dir,envir,param_val,sep = "")]
  }
  if(sys_params[i,param_type] == "root path"){
    sys_params[i,param_val := paste(substr(dir,1,nchar(dir)-1),param_val,sep = "")]
  }
}

cat(paste("Loaded",nrow(sys_params),"parameters","\n"))

and the console output when the batch file is called:

C:\Users\me\Desktop> cd /d W:\r\dev\

W:\r\dev>"C:Program Files\R\R-3.5.0\bin\i386\Rscript.exe" scripts\some_function.R
Warning message:
package 'data.table' was built under R version 3.5.1
directory = W:/r/
environment = dev
Loading system parameters ...
Loaded 32 parameters
directory = W:/r/
environment = dev
Loading system parameters ...
Loaded 32 parameters
directory = W:/r/
environment = dev
Loading system parameters ...
Loaded 32 parameters

What am I missing?

Rookatu
  • 1,487
  • 3
  • 21
  • 50
  • 1
    Does this happen regardless of what's in the file? If the script just had `cat("Hello")` would it still repeat? – MrFlick Apr 24 '19 at 16:16
  • 2
    I am unable to reproduce: double clicking batch, running in PowerShell, etc. Can you set up a reproducible example? Other than that something in the rest of your code may be triggering the repeat calls. Maybe a `source()` line? – Parfait Apr 24 '19 at 16:30
  • After doing some investigation I've narrowed the problem down to a `source()` call, as indicated by @Parfait (thanks!). Each of the scripts in the folder containing the called script is sourced as this folder contains the scripts needed for the code. Is there any way to make this work without moving this one script to its own folder (which would perturb our structure a bit)? – Rookatu Apr 24 '19 at 18:20

0 Answers0