Capturing actionButton shortcut after clicking it

Is it possible to grab the actionButton label after being 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 .R server.

One caveat is that buttons are dynamically created on the server. R with dynamic shortcuts (thus needing to grab the label when pressed)

thank

+1


source to share


2 answers


1) What button was clicked by the last user?

To answer this, you can use a user function observeEvent

and by setting a variable with a function reactiveValues

. Make sure you update your libraries and work with the latest version R (version 3.1.3)

as it shiny

depends on that version. While working on windows you can follow the 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 clicks for further manipulations below

Here's a small example based on the work of jdharrison from Shiny Interface: Saving changes to inputs and shinyStorage

.

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

      

+1


source


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

0


source







All Articles