Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create and export as.nanotime #47

Closed
jangorecki opened this issue Jun 4, 2019 · 9 comments
Closed

create and export as.nanotime #47

jangorecki opened this issue Jun 4, 2019 · 9 comments

Comments

@jangorecki
Copy link

jangorecki commented Jun 4, 2019

It might be sometimes useful to operate on arbitrary type objects. For example if fread's colClasses arg will be supplied with "nanotime" type, then we could flexibly dispatch to arbitrary class by using

convert = function(x, type) {
  as.call(c(as.name(paste("as", type, sep=".")), as.name("x")))
}
convert(x, type="nanotime")
#as.nanotime(x)

as.* functions are kind of standard in R.

@lsilvest
Copy link
Collaborator

lsilvest commented Jun 5, 2019

If I understand correctly, the end goal would be something like this, with a conversion from character to nanotime?

fread("a_file_that_contains_nanotimes.csv", colClasses="nanotime")

@jangorecki
Copy link
Author

fread was just an example of a function that could utilise as.nanotime.
source data type doesn't matter as long as there are methods to dispatch, as.nanotime.character, and most importantly as.nanotime.deafult.

@eddelbuettel
Copy link
Owner

eddelbuettel commented Jun 5, 2019

For reasons I no longer remember our constructor was always just nanotime() so we could possibly simply alias and register. We do register S3 methods for the other direction:

S3method(as.Date,nanotime)
S3method(as.POSIXct,nanotime)
S3method(as.POSIXlt,nanotime)
S3method(as.data.frame,nanotime)
S3method(as.integer64,nanotime)

@lsilvest
Copy link
Collaborator

lsilvest commented Jun 6, 2019

The reason I was asking for the end goal is that I'm not sure if there's a consensus that as.to_type.from_type be provided. The generic coercion function for an S4 class is as (by the way not yet implemented either for nanotime). So I don't think the convert function can expect full generality as is, especially for S4 classes. I'll try to dig into this a bit deeper...

@jangorecki
Copy link
Author

jangorecki commented Jun 19, 2019

@lsilvest regarding fread and as.* methods I mentioned, related point Rdatatable/data.table@1de0399#diff-8312ad0561ef661716b48d09478362f3R11

colClasses now supports 'complex', 'raw', 'Date', 'POSIXct', and user-defined classes (so long as an as. method exists)

@jangorecki
Copy link
Author

example where as.nanotime could be helpful: Rdatatable/data.table#4101

@eddelbuettel
Copy link
Owner

I am sorry but can you be more specific? I read the #4101 link but it "didn't click".

@jangorecki
Copy link
Author

Provided wrapper function calls as.numeric / as.integer / etc based on values in colClasses by calling do.call(paste0("as.", colClasses[[i]]), ...), thus if user wants a column to be nanotime then colClasses(time="nanotime", ...) allows to automatically convert a column to nanotime.

@lsilvest
Copy link
Collaborator

Yes, I agree we should provide the as.nanotime methods. I think Dirk's suggestion to alias should work well. Furthermore, I think we should in parallel provide the as type coercion via setAs functions. This would provide generality in type conversion for nanotime whichever way a users chooses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants