S3 class with changeable attribute causes subsetting error

Open
#3,388 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

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

non-atomic column

# 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.