Skip to content

Commit a7edaed

Browse files
committed
📇 Add initial hrg table
1 parent 060f2e1 commit a7edaed

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

R/mod_inequalities_server.R

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#' @noRd
44
mod_inequalities_server <- function(id, params) {
55
shiny::moduleServer(id, function(input, output, session) {
6+
ns <- session$ns
7+
68
inequalities_data <- shiny::reactive({
79
dataset <- shiny::req(params$dataset) # nolint: object_usage_linter
810

@@ -12,6 +14,88 @@ mod_inequalities_server <- function(id, params) {
1214
)
1315
})
1416

17+
# Initialize reactiveValues with NULL
18+
hrg <- reactiveValues(
19+
selections = NULL
20+
)
21+
22+
# Initialise data once inequalities_data is available
23+
observe({
24+
req(inequalities_data())
25+
26+
if (is.null(hrg$selections)) {
27+
hrg$selections <- tibble::tibble(
28+
hrg_code = unique(inequalities_data()$sushrg_trimmed),
29+
choice = "No change"
30+
)
31+
}
32+
})
33+
34+
# Handle "Set all to zero sum" button
35+
observeEvent(input$set_all_zero_sum, {
36+
hrg$selections$choice <- "Zero-sum"
37+
})
38+
39+
# Handle "Clear all" button
40+
observeEvent(input$clear_all, {
41+
hrg$selections$choice <- "No change"
42+
})
43+
44+
output$hrg_table <- DT::renderDataTable({
45+
# Create dropdown options for Choice column
46+
choice_options <- c("No change", "Zero-sum", "Level up", "Level down")
47+
48+
# Create the dropdown HTML for each row
49+
dropdown_html <- sapply(seq_len(nrow(hrg$selections)), function(i) {
50+
current_choice <- hrg$selections$choice[i]
51+
options_html <- glue::glue_collapse(
52+
sapply(choice_options, function(option) {
53+
selected <- if (option == current_choice) "selected" else ""
54+
glue::glue("<option value='{option}' {selected}>{option}</option>")
55+
})
56+
)
57+
glue::glue(
58+
"<select class='choice-select' data-row='{i}'>{options_html}</select>"
59+
)
60+
})
61+
62+
# Replace the choice column with dropdown HTML
63+
display_data <- hrg$selections
64+
display_data$choice <- dropdown_html
65+
66+
DT::datatable(
67+
display_data,
68+
escape = FALSE,
69+
rownames = FALSE,
70+
selection = "none",
71+
filter = "top",
72+
options = list(
73+
pageLength = 25,
74+
searching = FALSE,
75+
ordering = TRUE,
76+
info = TRUE
77+
),
78+
callback = DT::JS(glue::glue(
79+
"
80+
table.on('change', '.choice-select', function() {{
81+
var row = $(this).data('row');
82+
var value = $(this).val();
83+
Shiny.setInputValue('{ns('choice_changed')}', {{row: row, value: value}}, {{priority: 'event'}});
84+
}});
85+
"
86+
))
87+
)
88+
})
89+
90+
# Handle dropdown changes
91+
observeEvent(input$choice_changed, {
92+
row_index <- input$choice_changed$row
93+
new_value <- input$choice_changed$value
94+
95+
# Update the data
96+
hrg$selections$choice[row_index] <- new_value
97+
})
98+
1599
output$download_inequalities <- shiny::downloadHandler(
16100
filename = \() glue::glue("{params[['dataset']]}_inequalities.csv"),
17101
content = \(file) {

R/mod_inequalities_ui.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ mod_inequalities_ui <- function(id) {
2222
collapsible = FALSE,
2323
headerBorder = FALSE,
2424
width = 8,
25-
shiny::downloadButton(
26-
ns("download_inequalities"),
27-
"Download inequalities"
28-
)
25+
div(
26+
shiny::downloadButton(
27+
ns("download_inequalities"),
28+
"Download inequalities"
29+
),
30+
actionButton(ns("set_all_zero_sum"), "Set all to zero sum"),
31+
actionButton(ns("clear_all"), "Clear all", class = "btn-secondary")
32+
),
33+
DT::dataTableOutput(ns("hrg_table"), height = "calc(100vh - 200px)")
2934
)
3035
)
3136
)

0 commit comments

Comments
 (0)