Skip to content

Commit

Permalink
also a method for as.data.table.lints (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Jul 4, 2023
1 parent 6f1a924 commit ba896ef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Suggests:
tibble,
tufte,
withr (>= 2.5.0)
Enhances:
data.table
VignetteBuilder:
knitr
Config/Needs/website: tidyverse/tidytemplate
Expand Down
5 changes: 5 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ as_tibble.lints <- function(x, ..., # nolint: object_name_linter.
tibble::as_tibble(as.data.frame(x), ..., .rows = .rows, .name_repair = .name_repair, rownames = rownames)
}

as.data.table.lints <- function(x, keep.rownames = FALSE, ...) { # nolint: object_name_linter.
stopifnot(requireNamespace("data.table", quietly = TRUE))
data.table::setDT(as.data.frame(x), keep.rownames = keep.rownames, ...)
}

#' @export
`[.lints` <- function(x, ...) {
attrs <- attributes(x)
Expand Down
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,8 @@ settings <- NULL
if (requireNamespace("tibble", quietly = TRUE)) {
registerS3method("as_tibble", "lints", as_tibble.lints, asNamespace("tibble"))
}
if (requireNamespace("data.table", quietly = TRUE)) {
registerS3method("as.data.table", "lints", as.data.table.lints, asNamespace("data.table"))
}
}
# nocov end
8 changes: 8 additions & 0 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,11 @@ test_that("as_tibble.list is _not_ dispatched directly", {
1L
)
})

test_that("as.data.table.list is _not_ dispatched directly", {
skip_if_not_installed("data.table")
expect_identical(
nrow(data.table::as.data.table(lint(text = "a = 1", linters = assignment_linter()))),
1L
)
})

0 comments on commit ba896ef

Please sign in to comment.