Skip to content

Commit 08d0b17

Browse files
Add optional= argument to as.data.frame.ITime to interact better with data.frame() (#7108)
* Add optional= argument to as.data.frame.ITime to interact better with data.frame() * direct test of as.data.frame change
1 parent 6029f2f commit 08d0b17

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
7171
14. `data.table()` function is now more aligned with `data.frame()` with respect to the names of the output when one of its inputs is a single-column matrix object, [#4124](https://github.com/Rdatatable/data.table/issues/4124). Thanks @PavoDive for the report and @jangorecki for the PR.
7272
73+
15. Including an `ITime` object as a named input to `data.frame()` respects the provided name, i.e. `data.frame(a = as.ITime(...))` will have column `a`, [#4673](https://github.com/Rdatatable/data.table/issues/4673). Thanks @shrektan for the report and @MichaelChirico for the fix.
74+
7375
### NOTES
7476
7577
1. Continued work to remove non-API C functions, [#6180](https://github.com/Rdatatable/data.table/issues/6180). Thanks Ivan Krylov for the PRs and for writing a clear and concise guide about the R API: https://aitap.codeberg.page/R-api/.

R/IDateTime.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ as.character.ITime = format.ITime = function(x, ...) {
209209
res
210210
}
211211

212-
as.data.frame.ITime = function(x, ...) {
212+
as.data.frame.ITime = function(x, ..., optional=FALSE) {
213213
# This method is just for ggplot2, #1713
214214
# Avoids the error "cannot coerce class '"ITime"' into a data.frame", but for some reason
215215
# ggplot2 doesn't seem to call the print method to get axis labels, so still prints integers.
@@ -219,7 +219,8 @@ as.data.frame.ITime = function(x, ...) {
219219
# ans = list(as.POSIXct(x,tzone="")) # ggplot2 gives "Error: Discrete value supplied to continuous scale"
220220
setattr(ans, "class", "data.frame")
221221
setattr(ans, "row.names", .set_row_names(length(x)))
222-
setattr(ans, "names", "V1")
222+
# require 'optional' support for passing back to e.g. data.frame() without overriding names there
223+
if (!optional) setattr(ans, "names", "V1")
223224
ans
224225
}
225226

inst/tests/tests.Rraw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21246,3 +21246,9 @@ test(2322.12, levels(fctr(c("b","a","c"), rev=NA)), error="TRUE or FALSE")
2124621246
test(2322.21, levels(fctr(c("b","a","c"), sort=TRUE)), c("a","b","c"))
2124721247
test(2322.22, levels(fctr(c("b","a","c"), sort=NA)), error="TRUE or FALSE")
2124821248
test(2322.31, levels(fctr(c("b","a","c"), rev=TRUE, sort=TRUE)), c("c","b","a"))
21249+
21250+
# data.frame() uses provided names of ITime inputs
21251+
it <- as.ITime('00:00:00')
21252+
test(2323.1, names(data.frame(COL = it)), "COL")
21253+
test(2323.2, names(data.frame(b = 1, COL = it)), c("b", "COL"))
21254+
test(2323.3, names(as.data.frame(it, optional=TRUE)), NULL)

0 commit comments

Comments
 (0)