I have made a shiny UI where I want people to be able to able to add an additional bin on a tab and then be able to drag an element from the first bucket on the tab into the second bucket.
I am able to populate the first bucket and then have the second bucket be added using ‘Add Bin’ button however I cannot drag an element from the first bucket into the second (or subsequent buckets).
Any assistance I would be grateful for.
library(shiny)
library(shinyWidgets) # For rank_list
# Example data to simulate list_numeric and numeric_variables_revalue
list_numeric <- list(
"Variable1" = c("Value1", "Value2", "Value3"),
"Variable2" = c("Value4", "Value5", "Value6")
)
numeric_variables_revalue <- names(list_numeric)
# Define UI
ui <- fluidPage(
h4("Text."),
# Create a placeholder for the tabs
uiOutput("dynamicTabs"),
# Save and Close button
actionButton("saveClose", "Save and Close")
)
# Define server logic
server <- function(input, output, session) {
# Reactive value to store the current state of bins for each tab
bins <- reactiveVal(lapply(seq_along(numeric_variables_revalue), function(i) {
list(
labels = list_numeric[[i]], # Initial labels
count = 1
)
}))
# Render UI for dynamic tabs
output$dynamicTabs <- renderUI({
do.call(tabsetPanel, lapply(seq_along(numeric_variables_revalue), function(i) {
tabPanel(
numeric_variables_revalue[i],
h2(numeric_variables_revalue[i]),
uiOutput(paste0("rank_list_ui_", i)),
actionButton(paste0("add_bin_", i), "Add Bin")
)
}))
})
# Render rank lists for each tab
observe({
lapply(seq_along(numeric_variables_revalue), function(i) {
output[[paste0("rank_list_ui_", i)]] <- renderUI({
lapply(seq_len(bins()[[i]]$count), function(j) {
rank_list(
text = paste("Bin", j),
labels = if (j == 1) bins()[[i]]$labels else NULL, # Fill first bin, others empty
input_id = paste0("rank_list_", i, "_", j)
)
})
})
})
})
# Observe Add Bin button click for each tab
observeEvent(input$add_bin_1, {
current_bins <- bins()
current_bins[[1]]$count <- current_bins[[1]]$count + 1
bins(current_bins)
})
observeEvent(input$add_bin_2, {
current_bins <- bins()
current_bins[[2]]$count <- current_bins[[2]]$count + 1
bins(current_bins)
})
# Observe Save and Close button click
observeEvent(input$saveClose, {
# Close the app
stopApp()
})
}
shinyApp(ui = ui, server = server)