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

add error message for class "difftime" #232

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## rlistings 0.2.9.9008
* Added an error message for listings with variables of `difftime` class.

## rlistings 0.2.9
* Added `truetype` font support based on new `formatters` api, by @gmbecker.
Expand Down
9 changes: 9 additions & 0 deletions R/rlistings_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ dflt_courier <- font_spec("Courier", 9, 1)
#' @export
#' @name listing_methods
print.listing_df <- function(x, widths = NULL, tf_wrap = FALSE, max_width = NULL, fontspec = NULL, col_gap = 3L, ...) {

# check classes
column_classes <- lapply(x, function(col) sapply(col, class))

# Check if any of the columns contain an element of class "difftime"
if (any(sapply(column_classes, function(col) any(col == "difftime")))) {
stop("Error: Listing contains variables of class 'difftime'. Please convert to character or factor.")
}

cat(
toString(
matrix_form(x, fontspec = fontspec, col_gap = col_gap),
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-listings.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,19 @@ testthat::test_that("split_into_pages_by_var works as expected", {
)
testthat::expect_identical(lsting, lsting_id)
})

testthat::test_that("appropriate error message returned for 'difftime' class", {
tmp_data <- ex_adae[1:100, ]
class(tmp_data$study_duration_secs) <- "difftime"

lsting <- as_listing(
tmp_data,
key_cols = c("USUBJID", "AGE"),
disp_cols = "study_duration_secs",
main_title = "title",
main_footer = "foot"
) %>%
split_into_pages_by_var("SEX", page_prefix = "Patient Subset - Sex")

testthat::expect_error(print(lsting))
})
Loading