Rscript调用错误,但不是R IDE [英] Error with Rscript call, but not R IDE
问题描述
dat $ dt:chr2014-10-072014-10-072014-10-072014-10- 07...
我需要将其转换为日期。 将此列转换为日期在Rstudio中正常工作,但在将该文件称为Rscript时不会。
日期转换调用:
dat $ as_date< - as.Date(dat $ run_date)
pre>
外壳调用(Mac OSX):
Rscript my_file。 R'my_thing'
来自Rstudio的电话:
系统(粘贴(Rscript my_file.R,'my_thing'))
错误消息(与Rstudio中的shell或Rscript调用相同):
as.Date.numeric中的错误(dat $ dt):
'origin'必须提供
调用:as.Date - > as.Date.numeric
执行停止
我已经尝试提供原点和格式相同的结果:
dat $ as_date< - as.Date(dat $ dt,format ='%Y-%m - %d')
dat $ as_date< - as.Date(dat $ dt,origin =1970-01-01)
为什么as.Date()转换在IDE中工作,但不能作为Rscript调用,如何解决?
编辑:
感谢您的输入,下面是脚本的相关部分整个事情是〜1000行)。它从文件的顶部直到执行停止的失败与as.Date()调用:
my_file.R:
#!/ usr / bin / R
suppressMessages(require(sqldf))
suppressMessages(require(dplyr))
($($)
args< - commandArgs(TRUE)
THING< - args [1]
sq < - dbConnect(SQLite(),dbname =db2.sqlite3)
dat< - dbGetQuery(conn = sq,sprintf('select * from db_table where db_thing =%s',paste(shQuote(THING),collapse = )))
dat< - filter(dat,!grepl('exclude',comment))
dat $ the_date< - as.Date dt)
我还编辑了Rscript调用,就像我包含args一样。
这是数据结构;没有可以看到的因素。
'data.frame':128 obs。的2个变量:
$ dt:chr2014-10-072014-10-072014-10-072014-10-07...
$评论: chr
日期字段的行为与R(例如使用SQL和python)。
解决方案事实证明,我的一个Rscript调用有一个空格(例如
Rscript我的东西
),使数据帧中没有数据,因此,$ code> as.Date()错误
我可以按照以下方式模拟错误(对原来的Dirk支持):
Rscript -e'print(as.Date(0,format =%Y-%m-%d))'
as.Date.numeric中的错误(0,format =%Y-%m-%d):
'origin'必须提供
调用:print - >日期 - > as.Date.numeric
执行停止
对于系统调用,我添加了shQuote()在Rscript调用中,解决了这个问题。
my_thing ='有空格的东西
系统(粘贴(Rscript my_file.R,shQuote(my_thing)))
TLDR:错误在Rscript的电话中,Dirk的建议将问题解决成最简单的形式是很好的建议。
I'm pulling some data from an SQLite database into a data frame in R. One of the fields is a date field, which appears as a character field once pulled in R (using sqldf package):
dat$dt : chr "2014-10-07" "2014-10-07" "2014-10-07" "2014-10-07" ...
I need to convert this back to a date. Converting this column to a date works fine when in Rstudio, but not when I call the file as an Rscript.
Date conversion call:
dat$as_date <- as.Date(dat$run_date)
Shell call (Mac OSX):
Rscript my_file.R 'my_thing'
Call from Rstudio:
system(paste("Rscript my_file.R, 'my_thing'"))
Error message (same from shell or Rscript call in Rstudio):
Error in as.Date.numeric(dat$dt) : 'origin' must be supplied Calls: as.Date -> as.Date.numeric Execution halted
I've tried supplying an origin and format with the same results:
dat$as_date <- as.Date(dat$dt, format = '%Y-%m-%d') dat$as_date <- as.Date(dat$dt, origin="1970-01-01")
Why would the as.Date() conversion work within the IDE, but not as an Rscript call and how can this be fixed?
Edit:
Thanks for the input so far, below is the relevant part of script (the whole thing is ~1000 lines). It goes from the top of the file until the failure where execution stops with the as.Date() call:
my_file.R:
#!/usr/bin/R suppressMessages(require(sqldf)) suppressMessages(require(dplyr)) suppressMessages(require(reshape2)) args <- commandArgs(TRUE) THING <- args[1] sq <- dbConnect(SQLite(), dbname="db2.sqlite3") dat <- dbGetQuery(conn = sq, sprintf('select * from db_table where db_thing=%s', paste(shQuote(THING),collapse=","))) dat <- filter(dat, !grepl('exclude', comment)) dat$the_date <- as.Date(dat$dt)
I've also edited the Rscript call, as I do include args.
Here is the data structure; no factors that I can see.
'data.frame': 128 obs. of 2 variables: $ dt: chr "2014-10-07" "2014-10-07" "2014-10-07" "2014-10-07" ... $ comment : chr "" "" "" "" ...
The date field does behave as expected outside of R (e.g. with SQL and python).
解决方案It turns out that one of my Rscript calls had a space in it (e.g.
Rscript my thing
), such that there was no data in the dataframe and thus, theas.Date()
error.I was able to simulate the error as follows (props to Dirk for the original):
Rscript -e 'print(as.Date(0, format="%Y-%m-%d"))' Error in as.Date.numeric(0, format = "%Y-%m-%d") : 'origin' must be supplied Calls: print -> as.Date -> as.Date.numeric Execution halted
For the system call, I added shQuote() within the Rscript call, which solved the problem.
my_thing = 'something with spaces' system(paste("Rscript my_file.R, shQuote(my_thing)"))
TLDR: The error was in the Rscript call, Dirk's advice to break the problem down into it's most simple form was excellent advice.
这篇关于Rscript调用错误,但不是R IDE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!