r-在闪亮的服务器模块内创建radioButton时,无法访问其值



My shinyapp是使用模块构建的,radioBox组件inputId = modelling_type是使用renderUI函数在服务器中创建的,并存储在outputId = modelling_type_ui

当我使用模块时,我在mod_ui中对ID进行了名称间隔,然后为了(尝试!(在mod_server中使用相同的名称空间函数,我通过ns <- parentsession$ns调用了它。这不会引发错误。但我现在希望通过input$modelling_type访问RadioBox的价值

这不起作用!所以我一定是错误地调用了这个值。

这是代码:


library(shiny)
library(shinyalert)
library(shinydashboard)
library(shinyjs)
library(tidyverse)
# modules ------------------------------------------
mod_ui <- function(id){

ns <- NS(id)

fluidPage(

uiOutput(outputId = ns("modelling_type_ui")),

textOutput(outputId = ns("capture"))

)

}
mod_server <- function(id, parentsession){

moduleServer(id,
function(input, output, server){

ns <- parentsession$ns

output$modelling_type_ui = renderUI({

print(input$modelling_type) # this should not be null

radioButtons(
inputId = ns("modelling_type"), 
label = "Choose a modelling technique",
choices = c("OLS",
"Bayesian"),
selected = "OLS")

})

output$capture = renderText({ paste0("modelling type selected:", input$modelling_type) })

})

}

# call app ---------------------------------------
# run app
ui <- function(){ mod_ui("mt") }
server <- function(input, output, session){ mod_server("mt", session) }
shinyApp(ui = ui, server = server)

感谢您的帮助。通常我只会在UI中调用radioButtons,并在服务器中使用updateradioButton函数,但我正在处理一个重复使用以下方法的遗留应用程序。

为了扩展我上面的评论,这里有一个MWE,我相信它可以满足您的需求。

我不知道你为什么要用uiOutputrenderUI。我认为它在您的实际用例中是需要的,但这里不需要。此外,没有必要对parentsession之类的内容进行繁琐的处理。

调试打印打印NULL的一个原因是,在尝试打印其值时,您尚未定义无线电组。

library(shiny)
library(tidyverse)
mod_ui <- function(id){
ns <- NS(id)
fluidPage(
uiOutput(outputId = ns("modelling_type_ui")),
textOutput(outputId = ns("capture"))
)
}
mod_server <- function(id) {
moduleServer(
id,
function(input, output, session){
ns <- session$ns

output$modelling_type_ui = renderUI({
radioButtons(
inputId = ns("modelling_type"),
label = "Choose a modelling technique",
choices = c("OLS","Bayesian"), 
selected = "OLS"
)
})

output$capture <- renderText({
paste0("modelling type selected: ", input$modelling_type)
})

rv <- reactive({
input$modelling_type
})
return(rv)
}
)
}
ui <- function() { 
fluidPage(
mod_ui("mt"),
textOutput("returnValue")
)
}
server <- function(input, output, session) { 
modValue <- mod_server("mt") 

output$returnValue <- renderText({
paste0("The value returned by the module is ", modValue())
})
}
shinyApp(ui = ui, server = server)

最新更新