Reputation: 75
My application generates both automatic Ui and can also add ui manually. I have used both renderUI and InsertUI in the main application to call shiny modules.
The normal use case of shiny module even with renderUI function in module server updates my values nicely. But when I am generating shiny modules by invoking insertUI and renderUI function, the input values(selectInput, numericInput) are not updating. I am not understanding the reason for this, can anyone answer my problem.
Here is my workable code
Problem observing Shiny Module update inputs
# Module UI
ui_module <- function(id){
ns <- NS(id)
fluidRow(
uiOutput(ns("Input_ui"))
)
}
# Server UI
server_module <- function(input,output,session){
ns <- session$ns
output$Input_ui <- renderUI({
list(
tags$div(id = ns("input_div"),numericInput(ns("Input"),"Number",NA,value = 537153, step = 1)),
tags$div(id = ns("input2_div"),numericInput(ns("Input2"),"Number2",NA,value=686575,step = 1))
)
})
## INPUTS are not updating ##
observe({
updateNumericInput(session,
"Input", "Number",value = 4, step = 1)
updateNumericInput(session,
"Input2", "Number2",value = 8.9, step = 1)
})
}
# App UI
ui <- fluidPage(
fluidRow(id = "Row",
uiOutput("ui"),
actionButton("add","ADD")
)
)
# Server UI
server <- function(input,output,session){
# Initiating counter
n <- 0
# One by one adding the modules
observeEvent(input$add,{
n <<- n + 1
panels <- paste0("panels_new",n)
insertUI("#Row",
"beforeEnd",
ui_module(panels))
callModule(server_module,panels)
})
# Generating a no of shiny modules based on the some table rows
output$ui <- renderUI({
n <- 2 # n will be nrow in my main app.
list <- as.list(1:n)
lapply(list, function(i){
panels <- paste0("panels",i)
fluidRow(
ui_module(panels)
)
})
})
observe({
n <- 2 # n will be nrow in my main app.
list <- as.list(1:n)
lapply(list, function(i){
panels <- paste0("panels",i)
callModule(server_module,panels)
})
})
}
shinyApp(ui,server)
Normal Use case of Shiny Module (updation works)
#Module UI
ui_module <- function(id){
ns <- NS(id)
fluidRow(
uiOutput(ns("Input_ui"))
)
}
#Module Server
server_module <- function(input,output,session){
ns <- session$ns
output$Input_ui <- renderUI({
list(
tags$div(id = ns("input_div"),numericInput(ns("Input"), "Number",NA,value = 537153, step = 1)),
tags$div(id = ns("input2_div"),numericInput(ns("Input2"),"Number2",NA,value = 686575, step =1))
)
})
observe({
updateNumericInput(session,
"Input", "Number",value = 4, step = 1)
updateNumericInput(session,
"Input2", "Number2",value = 8.9, step = 1)
})
}
#APP UI
ui <- fluidPage(
fluidRow(id = "Row",
ui_module("panels")
)
)
#APP Server
server <- function(input,output,session){
callModule(server_module,"panels")
}
shinyApp(ui,server)
Upvotes: 1
Views: 939
Reputation: 12461
Upvotes: 0