How to update button labels in R Shiny?

2020-04-08 13:32发布

The R Shiny website has a great example of how to update the labels and values of a variety of input types based on user input. However, I couldn't find anything for buttons. Specifically, how do I update the label for a button based on user input?

4条回答
Viruses.
2楼-- · 2020-04-08 14:13

You can dynamically create the Button like so, updating the labels at the same time:

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))
查看更多
Deceive 欺骗
3楼-- · 2020-04-08 14:17

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

查看更多
The star\"
4楼-- · 2020-04-08 14:23

If your button is already built, the solution is to use library "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,"')"))
查看更多
一夜七次
5楼-- · 2020-04-08 14:35

Also may be useful bsButton from shinyBS. It allows change button style almost completely. Tray next shiny app for example

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

Here you could make "danger large android" it's look pretty well :)

查看更多
登录 后发表回答