How to update button labels in R Shiny?

后端 未结 4 1886
独厮守ぢ
独厮守ぢ 2021-01-20 00:10

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 butto

相关标签:
4条回答
  • 2021-01-20 00:48

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

    0 讨论(0)
  • 2021-01-20 00:56

    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)
    

    0 讨论(0)
  • 2021-01-20 01:04

    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))
    
    0 讨论(0)
  • 2021-01-20 01:06

    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,"')"))
    
    0 讨论(0)
提交回复
热议问题