简体   繁体   中英

Error with Rscript call, but not R IDE

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 (eg with SQL and python).

One of the classic difference is that Rscript does not load the the (built-in) methods package. So my first suggestion was to add a library("methods") at the top of your script.

On closer inspection, that is not it:

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

Also, you have an error indicating as.Date.numeric which suggests that you may gave factors in one case and not the other. Given that we do not have your script, it is hard to say more.

It turns out that one of my Rscript calls had a space in it (eg Rscript my thing ), such that there was no data in the dataframe and thus, the as.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM