Skip to content
Merged
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
10 changes: 9 additions & 1 deletion R/descriptives.b.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)))
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/testdescriptives.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)
Expand All @@ -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)
}
})

Expand Down
Loading