diff --git a/R/descriptives.b.R b/R/descriptives.b.R index 363500e1..5297e866 100644 --- a/R/descriptives.b.R +++ b/R/descriptives.b.R @@ -627,7 +627,7 @@ descriptivesClass <- R6::R6Class( for (var in tableVars) table$addColumn(name=var, title=var, type="text", combineBelow=TRUE) - table$addColumn(name='counts', title=.('Counts'), type='number') + table$addColumn(name='counts', title=.('Counts'), type='integer') table$addColumn(name='pc', title=.('% of Total'), type='number', format='pc') table$addColumn(name='cumpc', title=.('Cumulative %'), type='number', format='pc') @@ -997,6 +997,14 @@ descriptivesClass <- R6::R6Class( table <- tables$get(var) freq <- freqs[[var]] + # the case values aren't available during init, so set the + # counts type here; only show decimals for non-integer counts + # (i.e. when weighted by non-integer weights) + if ( ! all(freq == round(freq), na.rm=TRUE)) { + countsColumn <- table$columns[['counts']] + countsColumn$.__enclos_env__$private$.type <- 'number' + } + tableVars <- c(var, splitBy) allLevels <- lapply(jmvcore::select(self$data, tableVars), levels) grid <- rev(expand.grid(rev(allLevels))) diff --git a/tests/testthat/testdescriptives.R b/tests/testthat/testdescriptives.R index 978c84af..a4f084e6 100644 --- a/tests/testthat/testdescriptives.R +++ b/tests/testthat/testdescriptives.R @@ -168,16 +168,19 @@ params <- list( list( weights = NULL, expected_counts = c(1, 1, 1, 1, 1), + expected_type = "integer", info = "No weights" ), list( weights = c(1, 2, 3, 4, 5), expected_counts = c(1, 2, 3, 4, 5), + expected_type = "integer", info = "Integer weights" ), list( weights = c(0.5, 1, 1.5, 2, 2.5), expected_counts = c(0.5, 1, 1.5, 2, 2.5), + expected_type = "number", info = "Non-integer weights" ) ) @@ -194,6 +197,10 @@ testthat::test_that("Weighted grouped frequency table is displayed correctly", { # THEN the counts in the frequency table are correct r <- desc$frequencies[[1]]$asDF testthat::expect_equal(r$counts, param$expected_counts, info = param$info) + + # AND the counts are only displayed as decimals for non-integer weights + countsType <- desc$frequencies[[1]]$getColumn("counts")$type + testthat::expect_equal(countsType, param$expected_type, info = param$info) } })