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

fix: Remove duplicate stats with warning #107

Merged
merged 3 commits into from
Nov 8, 2024
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
33 changes: 23 additions & 10 deletions R/mod_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,50 @@ mod_plots_server <- function(id, selected_concepts, selected_dates) {
)
)

summary_stats <- get_summary_stats()
monthly_counts <- get_monthly_counts()

# Check for duplicated rows, potentially a problem with the source data
if (nrow(summary_stats) > dplyr::n_distinct(summary_stats)) {
cli::cli_warn(c(
"Duplicate rows detected in summary stats. Only keeping distinct rows.",
"i" = "This might point to a problem with the source data"
))
summary_stats <- dplyr::distinct(summary_stats)
}


moduleServer(id, function(input, output, session) {
selected_concept_ids <- reactive(selected_concepts()$concept_id)

## Filter data based on selected concept and date range
monthly_counts <- reactive({
filtered_monthly_counts <- reactive({
req(length(selected_concept_ids()) > 0)
req(selected_dates)
get_monthly_counts() |>
monthly_counts |>
dplyr::filter(.data$concept_id %in% selected_concept_ids()) |>
filter_dates(selected_dates())
})

summary_stats <- reactive({
filtered_summary_stats <- reactive({
req(length(selected_concept_ids()) > 0)
get_summary_stats() |>
summary_stats |>
dplyr::filter(.data$concept_id %in% selected_concept_ids())
})

output$monthly_counts <- plotly::renderPlotly({
req(nrow(monthly_counts()) > 0)
monthly_count_plot(monthly_counts())
req(nrow(filtered_monthly_counts()) > 0)
monthly_count_plot(filtered_monthly_counts())
})

output$numeric_stats <- renderPlot({
req(nrow(summary_stats()) > 0)
stat_numeric_plot(summary_stats())
req(nrow(filtered_summary_stats()) > 0)
stat_numeric_plot(filtered_summary_stats())
})

output$categorical_stats <- renderPlot({
req(nrow(summary_stats()) > 0)
stat_categorical_plot(summary_stats())
req(nrow(filtered_summary_stats()) > 0)
stat_categorical_plot(filtered_summary_stats())
})
})
}
8 changes: 4 additions & 4 deletions tests/testthat/test-mod_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ test_that("mod_plots_server reacts to changes in the selected concept", {
selected_row <- list(concept_id = 3003573L, concept_name = "test")
mock_concept_row(selected_row) # update reactive value
session$flushReact()
expect_identical(unique(summary_stats()$concept_id), selected_row$concept_id)
expect_identical(unique(filtered_summary_stats()$concept_id), selected_row$concept_id)

selected_row2 <- list(concept_id = 4276526L, concept_name = "test")
mock_concept_row(selected_row2) # update reactive value
session$flushReact()
expect_identical(unique(summary_stats()$concept_id), selected_row2$concept_id)
expect_identical(unique(filtered_summary_stats()$concept_id), selected_row2$concept_id)
}
)
})
Expand All @@ -39,13 +39,13 @@ test_that("mod_plots_server reacts to changes in the selected date range", {
selected_dates <- c("2019-01-01", "2019-12-31")
mock_date_range(selected_dates)
session$flushReact()
expect_true(all(monthly_counts()$date_year == 2019))
expect_true(all(filtered_monthly_counts()$date_year == 2019))

## Case when no data for given range
selected_dates2 <- c("3019-01-01", "3019-12-31")
mock_date_range(selected_dates2)
session$flushReact()
expect_equal(nrow(monthly_counts()), 0)
expect_equal(nrow(filtered_monthly_counts()), 0)
}
)
})
Expand Down
Loading