【问题标题】:How to update button labels in R Shiny?如何更新 R Shiny 中的按钮标签?
【发布时间】:2015-02-04 06:35:46
【问题描述】:

R Shiny 网站上有一个great example,介绍如何根据用户输入更新各种输入类型的标签和值。但是,我找不到任何按钮。具体来说,如何根据用户输入更新按钮的标签?

【问题讨论】:

  • updateActionButton ?

标签: r button shiny reactive-programming


【解决方案1】:

updateActionButton:

ui <- fluidPage(
  actionButton('someButton', ""),
  textInput("newLabel", "new Button Label:", value = "some label")
)

server <- function(input, output, session) {

  observeEvent(input$newLabel, {
    updateActionButton(session, "someButton", label = input$newLabel)
  })
}

shinyApp(ui, server)

【讨论】:

    【解决方案2】:

    也可能有用bsButton 来自shinyBS。它几乎可以完全改变按钮样式。以托盘下一个闪亮的应用为例

    library(shiny)
    library(shinyBS)
    
    ui <- function() {
        fixedPage(
            br(),
            # Button settings----
            fluidRow(
    
                column(
                    3,
                    align = "center",
                    textInput(
                        "label", 
                        "Button label"
                    ),
                    actionButton(
                        "set_label",
                        "Set label"
                    )
                ),
    
                column(
                    3,
                    align = "center",
                    selectInput(
                        "style", 
                        "Button style", 
                        choices = c(
                            "default",
                            "primary", 
                            "success", 
                            "info", 
                            "warning", 
                            "danger"
                        )
                    ),
                    actionButton(
                        "set_style", 
                        "Set style"
                    )
                ),
    
                column(
                    3,
                    align = "center",
                    selectInput(
                        "size", 
                        "Button size", 
                        choices = c(
                            "extra-small",
                            "small", 
                            "default", 
                            "large"
                        )
                    ),
                    actionButton(
                        "set_size", 
                        "Set size"
                    )
                ),
    
                column(
                    3,
                    align = "center",
                    selectInput(
                        "icon", 
                        "Button icon",
                        selected = NULL,
                        choices = c(
                            "",
                            "android",
                            "apple"
                        )
                    ),
                    actionButton(
                        "set_icon", 
                        "Set icon"
                    )
                )
            ),
    
            br(),
            # Сhangeable button ----
            fluidRow(
                column(
                    12,
                    align = "center",
                    h3("Сhangeable button"),
                    shinyBS::bsButton(
                        "action",
                        label = "initial",
                        icon = icon(NULL)
                    )
                )
            )
        )
    }
    
    server = function(input, output, session){
    
        observeEvent(input$set_label, {
            shinyBS::updateButton(session, "action", label = input$label)
        })
    
        observeEvent(input$set_style, {
            shinyBS::updateButton(session, "action", style = input$style)
        })
    
        observeEvent(input$set_size, {
            shinyBS::updateButton(session, "action", size = input$size)
        })
    
        observeEvent(input$set_icon, {
            shinyBS::updateButton(session, "action", icon = icon(input$icon))
        })
    }
    
    runApp(list(ui = ui, server = server))
    

    在这里你可以制作“危险的大型机器人”,它看起来很不错:)

    【讨论】:

      【解决方案3】:

      如果您的按钮已经构建,解决方案是使用库“shinyjs”。

      library(shinyjs)
       # in UI section
      shinyjs::useShinyjs()
       # in Server section: call this function at any time to update the button label
      runjs(paste0("$('label[for=\"YourButtonId\"]').text('",TextVariable,"')"))
      

      【讨论】:

        【解决方案4】:

        您可以像这样动态创建按钮,同时更新标签:

        library(shiny)
        
        ui =(pageWithSidebar(
          headerPanel("Test Shiny App"),
          sidebarPanel(
            textInput("sample_text", "test", value = "0"),
            #display dynamic UI
            uiOutput("my_button")),
          mainPanel()
        ))
        
        server = function(input, output, session){
          #make dynamic button
          output$my_button <- renderUI({
            actionButton("action", label = input$sample_text)
          })
        }
        runApp(list(ui = ui, server = server))
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        • 2022-11-11
        • 2020-10-11
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        相关资源
        最近更新 更多