[英]Unable to access the value of radioButton when created inside a shiny server module
My shinyapp is build using modules, the radioBox component inputId = modelling_type
is created in the server, using a renderUI function and stored under outputId = modelling_type_ui
我的 Shinyapp 是使用模块构建的,radioBox 组件inputId = modelling_type
是在服务器中创建的,使用 renderUI function 并存储在outputId = modelling_type_ui
modelling_type_ui 下
As I'm using modules, I have name spaced my IDs in the mod_ui
, and then in order to (attempt!) to use the same name space function in the mod_server
I have called it via ns <- parentsession$ns
.当我使用模块时,我在mod_ui
中命名了我的 ID,然后为了(尝试!)在 mod_server 中使用相同的命名空间mod_server
我通过ns <- parentsession$ns
调用它。 This doesn't throw an error.这不会引发错误。 But I would now expect to access the value of the RadioBox via input$modelling_type
但我现在希望通过input$modelling_type
modelling_type 访问 RadioBox 的值
This isn't working.这是行不通的。 So I must be calling the value incorrectly.所以我一定是错误地调用了这个值。
Here is the code:这是代码:
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)
Any help appreciated.任何帮助表示赞赏。 Usually I would just call radioButtons in the UI, and use updateradioButtons function in the server, but I'm dealing with a legacy app which uses the below method repeatedly.通常我只会在 UI 中调用 radioButtons,并在服务器中使用 updateradioButtons function,但我正在处理一个重复使用以下方法的遗留应用程序。
To expand on my comment above, here is a MWE that I believe does what you want.为了扩展我上面的评论,这是一个 MWE,我相信它可以满足您的需求。
I'm not sure why you're using uiOutput
and renderUI
.我不确定您为什么使用uiOutput
和renderUI
。 I assume it's needed in your actual use case, but it's not needed here.我认为在您的实际用例中需要它,但这里不需要它。 Also, there's no need to muck about with parentsession
and the like.此外,没有必要为父母会话之类的事情而parentsession
。
One reason why your debug print prints NULL
is that you haven't defined the radio group at the time you try to print its value.您的调试打印打印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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.