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

adds ptype2 and cast support for vctrs #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Depends:
R (> 3.0.2)
Imports:
lifecycle,
methods
methods,
vctrs
Suggests:
covr,
DT,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ S3method(quantile,formattable)
S3method(rep,formattable)
S3method(sum,formattable)
S3method(unique,formattable)
S3method(vec_cast,formattable.formattable)
S3method(vec_ptype2,formattable.formattable)
export(accounting)
export(area)
export(as.datatable)
Expand Down Expand Up @@ -97,3 +99,5 @@ import(lifecycle)
importFrom(grDevices,col2rgb)
importFrom(stats,median)
importFrom(stats,quantile)
importFrom(vctrs,vec_cast)
importFrom(vctrs,vec_ptype2)
14 changes: 14 additions & 0 deletions R/formattable.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,17 @@ formattable.data.frame <- function(x, ..., formatter = "format_table",
list(formatter = formatter, format = list(...),
preproc = preproc, postproc = postproc))
}



#' @importFrom vctrs vec_ptype2 vec_cast
NULL
#' @export
vec_ptype2.formattable.formattable <- function(x, y, ...) {
x
}

#' @export
vec_cast.formattable.formattable <- function(x, to, ...) {
x
}
12 changes: 12 additions & 0 deletions tests/testthat/test-formattable.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,15 @@ test_that("is.formattable", {
expect_equal(is.formattable(1), FALSE)
expect_equal(is.formattable(formattable(1)), TRUE)
})

test_that("formattable vectors have the same type", {

expect_s3_class(vec_ptype2.formattable.formattable(currency(1:5),currency(1:5)),
c("formattable", "integer"))
})


test_that("vector casting of formattable types is allowed", {
expect_true(is.formattable(vec_cast.formattable.formattable(currency(1:5))))
expect_true(is.formattable(vec_cast.formattable.formattable(comma(1:5))))
})