Home > Back-end >  Unable to access the value of radioButton when created inside a shiny server module
Unable to access the value of radioButton when created inside a shiny server module

Time:01-23

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

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. This doesn't throw an error. But I would now expect to access the value of the RadioBox via input$modelling_type

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.

CodePudding user response:

To expand on my comment above, here is a MWE that I believe does what you want.

I'm not sure why you're using uiOutput and 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.

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.

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)
  •  Tags:  
  • Related