我的应用程序既可以自动生成 UI,也可以手动添加 ui。我在主应用程序中使用了 renderUI 和 InsertUI 来调用闪亮的模块。
即使在模块服务器中使用 renderUI 函数,闪亮模块的正常用例也能很好地更新我的值。但是当我通过调用 insertUI 和 renderUI 函数生成闪亮的模块时,输入值(selectInput,numericInput)没有更新。我不明白这是什么原因,谁能回答我的问题。
这是我的可行代码
观察闪亮模块更新输入的问题
# 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)
闪亮模块的正常用例(更新工作)
#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)