Capture the label of an actionButton once it is clicked

会有一股神秘感。 提交于 2019-12-02 04:31:24
Pork Chop

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

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!