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