Skip to content

add option to remove empty columns in tm_t_crosstable #890

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

Merged
merged 8 commits into from
Jul 8, 2025

Conversation

m7pr
Copy link
Contributor

@m7pr m7pr commented Jun 30, 2025

Closes https://github.com/insightsengineering/coredev-tasks/issues/363

devtools::load_all("teal.code")
devtools::load_all("teal.data")
devtools::load_all("teal.reporter")
devtools::load_all("teal.logger")
devtools::load_all("teal.transform")
devtools::load_all("teal.widgets")
devtools::load_all("teal")
devtools::load_all("teal.modules.general")




data <- teal_data()
data <- within(data, {
  mtcars <- mtcars
  for (v in c("cyl", "vs", "am", "gear")) {
    mtcars[[v]] <- as.factor(mtcars[[v]])
  }
  mtcars[["primary_key"]] <- seq_len(nrow(mtcars))
})
join_keys(data) <- join_keys(join_key("mtcars", "mtcars", "primary_key"))

app <- init(
  data = data,
  modules = modules(
    tm_t_crosstable(
      label = "Cross Table",
      x = data_extract_spec(
        dataname = "mtcars",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
          selected = c("cyl", "gear"),
          multiple = TRUE,
          ordered = TRUE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "mtcars",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
          selected = "vs",
          multiple = FALSE,
          fixed = FALSE
        )
      )
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}

1
2

@m7pr m7pr requested a review from donyunardi June 30, 2025 11:35
@m7pr m7pr added the core label Jun 30, 2025
Copy link
Contributor

github-actions bot commented Jun 30, 2025

badge

Code Coverage Summary

Filename                      Stmts    Miss  Cover    Missing
--------------------------  -------  ------  -------  ---------------------------------------
R/tm_a_pca.R                    889     889  0.00%    139-1161
R/tm_a_regression.R             773     773  0.00%    178-1056
R/tm_data_table.R               201     201  0.00%    100-349
R/tm_file_viewer.R              172     172  0.00%    47-254
R/tm_front_page.R               144     133  7.64%    77-247
R/tm_g_association.R            344     344  0.00%    159-578
R/tm_g_bivariate.R              698     434  37.82%   331-826, 867, 978, 995, 1013, 1024-1046
R/tm_g_distribution.R          1117    1117  0.00%    156-1420
R/tm_g_response.R               369     369  0.00%    177-625
R/tm_g_scatterplot.R            734     734  0.00%    260-1098
R/tm_g_scatterplotmatrix.R      297     278  6.40%    198-532, 593, 607
R/tm_missing_data.R            1119    1119  0.00%    124-1420
R/tm_outliers.R                1045    1045  0.00%    163-1359
R/tm_t_crosstable.R             285     285  0.00%    175-509
R/tm_variable_browser.R         803     798  0.62%    89-1044, 1082-1265
R/utils.R                       151     135  10.60%   87-272, 302-338, 350-359, 364, 378-397
R/zzz.R                           2       2  0.00%    2-3
TOTAL                          9143    8828  3.45%

Diff against main

Filename               Stmts    Miss  Cover
-------------------  -------  ------  --------
R/tm_t_crosstable.R      +21     +21  +100.00%
TOTAL                    +21     +21  -0.01%

Results for commit: 6f89792

Minimum allowed coverage is 80%

♻️ This comment has been updated with latest results

Copy link
Contributor

Unit Tests Summary

  1 files  22 suites   2s ⏱️
144 tests 29 ✅ 115 💤 0 ❌
182 runs  67 ✅ 115 💤 0 ❌

Results for commit 8dba181.

Copy link
Contributor

github-actions bot commented Jun 30, 2025

Unit Tests Summary

  1 files  22 suites   1s ⏱️
144 tests 29 ✅ 115 💤 0 ❌
182 runs  67 ✅ 115 💤 0 ❌

Results for commit 6f89792.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@donyunardi donyunardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we would automatically drop the columns with 0 observations, but I think it makes sense to include this as an encoding option.

Everything works as intended, however, show_percentage and show_total are arguments for this function and are part of the Table Settings element. Since we're adding remove_zero_columns to the Table Settings, I think it should also be an argument to keep it consistent with show_percentage and show_total.

@kumamiao would you agree?

@gogonzo gogonzo self-assigned this Jul 2, 2025
@kumamiao
Copy link

kumamiao commented Jul 3, 2025

Everything works as intended, however, show_percentage and show_total are arguments for this function and are part of the Table Settings element. Since we're adding remove_zero_columns to the Table Settings, I think it should also be an argument to keep it consistent with show_percentage and show_total.

@kumamiao would you agree?

Agree to add argument to be consistent across show_percentage, show_total, and remove_zero_columns.

@m7pr
Copy link
Contributor Author

m7pr commented Jul 3, 2025

Hey, added a parameter remove_zero_columns to maintain consistency.
Done in this commit 9481745

data <- teal_data()
data <- within(data, {
  mtcars <- mtcars
  for (v in c("cyl", "vs", "am", "gear")) {
    mtcars[[v]] <- as.factor(mtcars[[v]])
  }
  mtcars[["primary_key"]] <- seq_len(nrow(mtcars))
})
join_keys(data) <- join_keys(join_key("mtcars", "mtcars", "primary_key"))

app <- init(
  data = data,
  modules = modules(
    tm_t_crosstable(
      label = "Cross Table",
      x = data_extract_spec(
        dataname = "mtcars",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
          selected = c("cyl", "gear"),
          multiple = TRUE,
          ordered = TRUE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "mtcars",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
          selected = "vs",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      remove_zero_columns = TRUE
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}

@m7pr m7pr requested review from donyunardi, kumamiao and Copilot July 3, 2025 09:54
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new remove_zero_columns option to the cross-table module, allowing users to drop columns that contain only zeros.

  • Introduces remove_zero_columns parameter in both UI and server
  • Updates documentation (Rd and roxygen) with the new option
  • Implements conditional logic to drop empty columns before building the table

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
man/tm_t_crosstable.Rd Adds “Remove zero-only columns” under the Table Settings section
R/tm_t_crosstable.R Introduces remove_zero_columns argument, UI checkbox, server logic, and branching behavior
Comments suppressed due to low confidence (1)

R/tm_t_crosstable.R:169

  • The new remove_zero_columns option needs accompanying unit and integration tests to verify that empty columns are correctly removed when enabled and retained when disabled.
                            remove_zero_columns = FALSE,

@m7pr
Copy link
Contributor Author

m7pr commented Jul 4, 2025

@kumamiao @donyunardi did you have chance to review?

Copy link
Contributor

@donyunardi donyunardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add entry to NEWS.md before merging.

@m7pr m7pr enabled auto-merge (squash) July 8, 2025 07:16
@m7pr m7pr merged commit 3cd6c50 into main Jul 8, 2025
25 of 26 checks passed
@m7pr m7pr deleted the 363_crosstable@main branch July 8, 2025 07:23
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants