1

Is it possible to capture the label of an actionButton once it is clicked?

Imagine I have 3 buttons on my ui.R and depending on which one I click I want to perform a different action on the server.R.

One caveat is that the buttons are created dynamically on the server.R with dynamic labels (thus the necessity of capturing the label on click)

Thanks

Diego
  • 34,802
  • 21
  • 91
  • 134

2 Answers2

4

Something like that ?

library(shiny)

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

  output$printLabel <- renderPrint({input$btnLabel})

}

ui <- fluidPage(

  actionButton("btn1", "Label1", 
               onclick = "Shiny.setInputValue('btnLabel', this.innerText);"),
  actionButton("btn2", "Label2", 
               onclick = "Shiny.setInputValue('btnLabel', this.innerText);"),

  verbatimTextOutput("printLabel")  

)

shinyApp(ui = ui, server = server)

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
1

1) What button was clicked last by the user?

To answer this you can user observeEvent function and by setting up a a variable using reactiveValues function. Make sure you update your libraries and work in the latest version of R (version 3.1.3) as shiny is dependant on this version. Working on windows you can follow example on how to update here

rm(list = ls())
library(shiny)

ui =fluidPage(
  sidebarPanel(
    textInput("sample1", "Name1", value = "A"),
    textInput("sample2", "Name2", value = "B"),
    textInput("sample3", "Name3", value = "C"),
    div(style="display:inline-block",uiOutput("my_button1")),
    div(style="display:inline-block",uiOutput("my_button2")),
    div(style="display:inline-block",uiOutput("my_button3"))),
  mainPanel(textOutput("text1"))
)

server = function(input, output, session){

  output$my_button1 <- renderUI({actionButton("action1", label = input$sample1)})
  output$my_button2 <- renderUI({actionButton("action2", label = input$sample2)})
  output$my_button3 <- renderUI({actionButton("action3", label = input$sample3)})

  my_clicks <- reactiveValues(data = NULL)

  observeEvent(input$action1, {
    my_clicks$data <- input$sample1
  })

  observeEvent(input$action2, {
    my_clicks$data <- input$sample2
  })  

  observeEvent(input$action3, {
    my_clicks$data <- input$sample3
  })  

  output$text1 <- renderText({ 
    if (is.null(my_clicks$data)) return()
    my_clicks$data
  })
}
runApp(list(ui = ui, server = server))

2) Save the clicks for further manipulation is below

Here's small example based on the work of jdharrison from Shiny UI: Save the Changes in the Inputs and the shinyStorage package.

rm(list = ls())
#devtools::install_github("johndharrison/shinyStorage")
library(shinyStorage)
library(shiny)

my_clicks <- NULL

ui =fluidPage(
  #
  addSS(),
  sidebarPanel(
    textInput("sample_text", "test", value = "0"),
    uiOutput("my_button")),
  mainPanel(uiOutput("text1"))
)

server = function(input, output, session){
  ss <- shinyStore(session = session)

  output$my_button <- renderUI({
    actionButton("action", label = input$sample_text)
  })

  observe({
    if(!is.null(input$sample_text)){
      if(input$sample_text != ""){
        ss$set("myVar", input$sample_text)
      }
    }
  })  

  output$text1 <- renderUI({
    input$action
    myVar <- ss$get("myVar")
    if(is.null(myVar)){
      textInput("text1", "You button Name")
    }else{
      textInput("text1", "You button Name", myVar)          
    }
  })
}
runApp(list(ui = ui, server = server))
Community
  • 1
  • 1
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • thanks pops, but that's not what I'm looking for. I want to set the text on a component based on the label of the button. I used the code you posted here (http://stackoverflow.com/questions/27326929/how-to-update-button-labels-in-r-shiny/27458250#comment46948109_27458250) to dynamically create the buttons with a dynamic label (lets say A, B and C) and now I need to know on whihc button the user clicked. Thanks – Diego Apr 01 '15 at 10:10
  • Hi @Diego, did you ever figure out how to do this? I'm running into a similar problem ... Thanks. – Carlos Dec 02 '16 at 11:27