Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rscript调用出错,但不是R IDE_R_Date_Type Conversion - Fatal编程技术网

Rscript调用出错,但不是R IDE

Rscript调用出错,但不是R IDE,r,date,type-conversion,R,Date,Type Conversion,我正在将SQLite数据库中的一些数据拉入R中的数据框中。其中一个字段是日期字段,在拉入R后显示为字符字段(使用sqldf包): 我需要把它转换回日期在Rstudio中,将此列转换为日期可以正常工作,但在我将文件作为Rscript调用时则不行。 日期转换电话: dat$as_date <- as.Date(dat$run_date) Rstudio来电: system(paste("Rscript my_file.R, 'my_thing'")) 错误消息(与Rstudio中的shel

我正在将SQLite数据库中的一些数据拉入R中的数据框中。其中一个字段是日期字段,在拉入R后显示为字符字段(使用sqldf包):

我需要把它转换回日期在Rstudio中,将此列转换为日期可以正常工作,但在我将文件作为Rscript调用时则不行。

日期转换电话:

dat$as_date <- as.Date(dat$run_date)
Rstudio来电:

system(paste("Rscript my_file.R, 'my_thing'"))
错误消息(与Rstudio中的shell或Rscript调用相同):

我已尝试提供具有相同结果的来源和格式:

dat$as_date <- as.Date(dat$dt, format = '%Y-%m-%d')
dat$as_date <- as.Date(dat$dt, origin="1970-01-01")

日期字段在R之外的行为与预期一致(例如,使用SQL和python)。

一个典型的区别是
Rscript
不加载(内置)方法包。 因此,我的第一个建议是在脚本顶部添加一个
库(“方法”)

仔细观察,情况并非如此:

edd@max:~$ Rscript -e 'print(as.Date("2014-01-07", format="%Y-%m-%d"))'     
[1] "2014-01-07"
edd@max:~$ 

此外,您还有一个错误指示
为.Date.numeric
,这表明您可能在一种情况下给出了因子,而不是在另一种情况下给出了因子。考虑到我们没有您的脚本,很难说得更多。

事实证明,我的一个Rscript调用中有一个空格(例如,
Rscript my thing
),因此数据帧中没有数据,因此出现了
as.Date()
错误

我能够模拟如下错误(道具到德克为原始):

对于系统调用,我在Rscript调用中添加了shQuote(),解决了这个问题

my_thing = 'something with spaces'

system(paste("Rscript my_file.R, shQuote(my_thing)"))

TLDR:错误在Rscript调用中,德克将问题分解为最简单的形式的建议是非常好的建议。

发布整个脚本。您没有提供足够的信息来诊断问题。
as.Date
函数是泛型函数,数值方法和字符方法有不同的参数。谢谢Dirk,我提供了更多的细节。我也注意到了
as.Date.numeric
,但无法说明为什么它会被用于似乎是字符向量的内容。请将其进一步隐藏。检索一些数据,对其执行
dput()
,并简化脚本,直到没有错误——或者没有进一步的
library()
调用。我们无法测试,因为我们没有您的数据库。谢谢,您的建议帮助我缩小了范围并解决了问题。我把它贴出来作为一个答案,以防有人遇到它。是的,当您将数字输入数据作为.Date(),则必须有一个来源。
dat$as_date <- as.Date(dat$dt, format = '%Y-%m-%d')
dat$as_date <- as.Date(dat$dt, origin="1970-01-01")
#!/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)
 'data.frame':  128 obs. of  2 variables:
 $ dt: chr  "2014-10-07" "2014-10-07" "2014-10-07" "2014-10-07" ...
 $ comment : chr  "" "" "" "" ...
edd@max:~$ Rscript -e 'print(as.Date("2014-01-07", format="%Y-%m-%d"))'     
[1] "2014-01-07"
edd@max:~$ 
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
my_thing = 'something with spaces'

system(paste("Rscript my_file.R, shQuote(my_thing)"))