Search code examples
rshinymoduleshinymodules

shiny module: update color of button


i am struggling with the logic of updating a button in the ui based on code that is living in a module. When I click the button i want it to change the colour but nothing happens. I understand to make ui input reactive in order to use it in the server module but i do not get it working the other way around. Help (and some explanation) is highly appreciated!

here is my code: (I left the save to csv part upon click as i got that to work).

library(shiny)
library(shinyBS)

module_company_ui<-function(id){
  # `NS(id)` returns a namespace function, which was save as `ns` and will
  # invoke later.
  ns <- NS(id)
  
  tagList(
    radioButtons(ns("company_name"), label= "Is the << COMPANY NAME >> correct?",
                 c("choose from below" = "10",
                   "correct" = "0.15",
                   "correct, but some noise" = "0.075",
                   "not sure" = "0.05",
                   "wrong" = "0"),selected = character(0)),
    textOutput(ns("txt")),
    bsButton(ns('save_inputs'), " Save", type="action", block=TRUE, style="info", icon=icon(name="save"))
    
  )
}


module_save_inputs<-function(id, value_company){
  moduleServer(
    id,
    ## Below is the module function
    function(input, output, session) {
      save<-reactive(input$save_inputs)
      observeEvent(save(), {
        updateButton(session, "save_inputs",label = " Save", block = T, style = "success", icon=icon(name="save"))
      })
    })
}

ui <- fluidPage(
  module_company_ui("company")
)
server <- function(input, output, session) {

  module_save_inputs("company")
}

shinyApp(ui, server)



Solution

  • You need namespace ns for the updateButton. Try this

    module_save_inputs<-function(id, value_company){
      moduleServer(
        id,
        ## Below is the module function
        function(input, output, session) {
          ns <- session$ns
          save<-reactive(input$save_inputs)
          observeEvent(save(), {
            updateButton(session, ns("save_inputs"),label = " Save", block = T, style = "success", icon=icon(name="save"))
          })
        })
    }