I am utilizing Shiny modules to define the user interface (UI) and server components for parameter modules as well as for a datatable module. The desired behavior is that the datatable should be generated or filtered only when the submit button is pressed.
The current behavior is as follows: When the app is initially run, it functions correctly, and the datatable is not generated until the submit button is pressed. However, if I change the input parameters a second time, the datatable is generated without requiring the submit button to be pressed.
The desired behavior is to consistently require the submit button to be pressed for the datatable to be generated, regardless of whether input parameters are changed.
here is an example which gives this behaviour
data <- data.frame(
Input1 = c(1,2,3),
Input2 = c(1,2,3),
Input3 = c(1,2,3)
)
param_ui <- function(id) {
ns <- NS(id)
tagList(
numericInput(ns("input1"), "Input 1", value = 0),
numericInput(ns("input2"), "Input 2", value = 0),
numericInput(ns("input3"), "Input 3", value = 0),
actionButton(ns("submit"), "Submit")
)
}
param_server <- function(id) {
moduleServer(id,function(input, output, session) {
return(list(
input1 = reactive({input$input1}),
input2 = reactive({input$input2}),
input3 = reactive({input$input3}),
submit = reactive({input$submit})
))
})
}
datatable_ui <- function(id) {
ns <- NS(id)
tagList(
dataTableOutput(ns("table"))
)
}
datatable_server <- function(id,input1, input2,input3) {
moduleServer(id,function(input, output, session) {
data1 <- reactive({data %>% filter(Input1 == input1() & Input2 == input2() & Input3 == input3())})
output$table <- renderDataTable(data1())
})
}
ui <- fluidPage(
param_ui(id = "param"),
datatable_ui(id = "datatb")
)
server <- function(input, output) {
param <- param_server(id = "param")
observeEvent(param$submit(), {
datatable_server(id = "datatb",
input1 = param$input1,
input2 = param$input2,
input3 = param$input3)
})
}
shinyApp(ui = ui, server = server)
One way is shown below.