我的Windows桌面上有一个批处理文件test.bat
,其中包含以下内容:
cd /d W:\r\dev\
"C:\Program Files\R\R-3.5.0\bin\i386\Rscript.exe" scripts\some_function.R
执行后,引用的R脚本some_function.R
开始运行,并向控制台输出消息,让我知道它在哪里。但是,它会在到达R代码中的某个点后停止,然后重新开始并继续这样做,直到会话自动终止。
我检查了R代码,以了解为什么批处理文件将继续返回到开头,但没有找到。直接运行R文件(例如,从R studio内部运行)可以正常工作。
我很清楚naming the batch file with a command that appears in the file can be problematic,但事实并非如此。我为批处理文件尝试了多个名称,但这似乎无法解决问题。
下面是被引用文件some_function.R
中代码的开头,该文件不断被重新执行:
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"))
以及调用批处理文件时的控制台输出:
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
我想念什么?