S3 class with changeable attribute causes subsetting error
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
Research direction
Start by running the minimal MyDate example and compare DT[2, x] with DF[2, 1]. Trace the [.MyDate and [[.MyDate entry points together with data.table column subsetting, then add a regression test showing that the selected value and myattr are preserved. Done means data.table subsetting matches the demonstrated data.frame behavior for this class.
Written by the indexing model from the issue text.
Description
# Minimal reproducible example
Considering this example below. Here a new class named MyDate is created, which inherits from Date and uses an attribute named myattr to store some meta data.
MyDate <- function (x, myattr = 1L) {
structure(x, myattr = rep_len(myattr, length(x)), class = c("MyDate", "Date"))
}
`[.MyDate` <- function (x, i) {
r <- NextMethod("[")
data.table::setattr(r, "myattr", attr(x, "myattr")[i])
data.table::setattr(r, "class", c("MyDate", "Date"))
r
}
`[[.MyDate` <- function (x, i) {
r <- NextMethod("[[", i)
data.table::setattr(r, "myattr", attr(x, "myattr")[[i]])
data.table::setattr(r, "class", c("MyDate", "Date"))
r
}
format.MyDate <- function (x, ...) {
a <- attr(x, "myattr")
res <- rep(NA_character_, length(x))
res[a == 0L] <- "0"
res[a == 1L] <- as.character(data.table::yday(x[a == 1L]))
res[a == 2L] <- format.Date(x[a == 2L], "%m-%d")
res[a == 3L] <- format.Date(x[a == 3L], "%Y-%m-%d")
res
}
x <- MyDate(0:3, 0:3)
x
# [1] "0" "2" "01-03" "1970-01-04"
x[2]
# [1] "2"
attr(x[2], "myattr")
# [1] 1
x[[2]]
# [1] "2"
attr(x[[2]], "myattr")
# [1] 1
Everything works fine so far. However, if I put x in a data.table, x column cannot be correctly subsetted.
DT <- data.table::data.table(x)
# x
# 1: 0
# 2: 2
# 3: 01-03
# 4: 1970-01-04
x1 <- DT[2, x]
x1
# [1] "0" NA NA NA
attr(x1, "myattr")
# [1] 0 1 2 3
But data.frame works fine:
DF <- as.data.frame(DT)
DF
# x
# 1 0
# 2 2
# 3 01-03
# 4 1970-01-04
x2 <- DF[2,1]
x2
# [1] "2"
attr(x2, "myattr")
# [1] 1
# Output of sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux
Matrix products: default
BLAS: /usr/lib/libblas.so.3.8.0
LAPACK: /usr/lib/liblapack.so.3.8.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=zh_CN.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] nvimcom_0.9-80 colorout_1.2-0
loaded via a namespace (and not attached):
[1] compiler_3.5.2 tools_3.5.2 data.table_1.11.8
Any insight will be greatly appreciated.
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·