闪亮模块:更新按钮的颜色

时间:2021-04-18 11:50:51

标签: r shiny module shinymodules

我正在努力解决基于模块中的代码更新用户界面中按钮的逻辑。当我单击按钮时,我希望它改变颜色,但没有任何反应。我理解为了在服务器模块中使用它而使 ui 输入具有反应性,但我不让它以相反的方式工作。非常感谢帮助(和一些解释)!

这是我的代码:(当我开始工作时,我将保存到 csv 部分留在了点击)。

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)


1 个答案:

答案 0 :(得分:1)

您需要 ns 的命名空间 updateButton。试试这个

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"))
      })
    })
}
相关问题