自动化脚本,Rscripts

时间:2013-08-14 11:35:47

标签: windows r scheduled-tasks rscript

我在自动化.R文件方面遇到了很多麻烦,而且我无法理解有关它的信息。但是这里有:

我正在使用Windows 7,只想在每天早上08.00自动运行一个R.script。 .R文件自己吐出输出,所以我不想要一个单独的输出文件。我已经创建了一个这样的bat文件:

"C:\R\R-3.0.1\bin\x64\Rscript.exe" "C:\R\R-3.0.1\bin\x64\Scripts\24AR_v1bat.R"
Echo %DATE% %TIME% %ERRORLEVEL% >> C:\R\R-3.0.1\bin\x64\scripts\24AR_v1.txt

当我手动运行时,它完美运行。有/无:

--default-packages=list

当我通过cmd窗口运行它时,它运行得很好。然而,当我尝试通过任务调度程序运行它时,它运行,但不起作用。 (我在错误消息文件中得到1或2错误)。

我看过R简介 - 从命令行调用R和帮助(Rscript),但我仍然无法让它工作。

NEW EDIT:我发现没有进行MS SQL调用,会让我的代码从调度程序运行。不确定我是否应该提出新问题或?

编辑:添加R脚本

# 24 Hour AR-model, v1 ----------------------------------------------------
#Remove all variables from the workspace
#rm(list=ls())
# Loading Packages
library(forecast)

#Get spot-prices System from 2012-01-01 to today
source("/location/Scripts/SQL_hourlyprices.R")
sys <- data.frame()
sys <- spot
rm(spot)

# Ordering the data, first making a matrix with names: SYS
colnames(sys) <- c("date","hour","day","spot")
hour <-factor(sys[,2])
day <-factor(sys[,3]) 
dt<-sys[,1]
dt<-as.Date(dt)
x<-sys[,4]

q <-ts(x, frequency=24)

x0<- q[hour==0]
x1<- q[hour==1]

x0 <-ts(x0, frequency=7)
x1 <-ts(x1, frequency=7)

# ARIMA MODELS
y0<-Arima(x0,order=c(2,1,0))
y1<-Arima(x1,order=c(2,1,1))

fr0 <- forecast.Arima(y0,h=1)
fr1 <- forecast.Arima(y1,h=1)

h1<-as.numeric(fr0$mean)
h2<-as.numeric(fr1$mean)

day1 <-Sys.Date()+1
atable<-data.frame
runtime<-Sys.time()
atable<-cbind(runtime,day1,h1,h2)
options(digits=4)

write.table(atable,   file="//location/24ar_v1.csv", 
append=TRUE,quote=FALSE, sep=",", row.names=F, col.names=F)

但正如我所说,我可以使用批处理文件手动运行代码并使其完美运行,但是使用调度程序它将无法正常工作。

1 个答案:

答案 0 :(得分:1)

经过几个小时的尝试,似乎问题在于我:

source("/location/Scripts/SQL_hourlyprices.R")

我只是在里面进行了SQL调用:

sqlQuery(dbdata2, "SELECT CONVERT(char(10), [lokaldatotid],126) AS date, 
   DATEPART(HOUR,lokaldatotid) as hour,
   DATENAME(DW,lokaldatotid) as dag,
   pris as spot

     FROM [SpotPriser] vp1
     WHERE  (vp1.boers_id=0)
     AND (vp1.omraade_id=0)
     AND lokaldatotid >='2012-01-01'
     GROUP BY lokaldatotid, pris
     ORDER BY lokaldatotid, hour desc") -> spot

当我将其直接移动到脚本中并删除源代码行时,脚本将与调度程序一起运行。

我不知道为什么......

相关问题