如何更新R Shiny中的按钮标签?

时间:2014-12-06 00:38:29

标签: r button shiny reactive-programming

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

4 个答案:

答案 0 :(得分:5)

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

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))

答案 1 :(得分:4)

如果您的按钮已经构建,解决方案是使用库&#34; shinyjs&#34;。

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,"')"))

答案 2 :(得分: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)

enter image description here

答案 3 :(得分:0)

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))

在这里您可以制作“危险的大型android系统”,看起来不错:)