1

What's the best way to get the label of an actionButton that is clicked? I have an actionButton that has the label updated. When the user clicks, I need to capture that. I tried input$action2.label and input$action2.text, but neither worked.

ui.R

library(shiny)

shinyUI( fluidPage(

tags$head(tags$style(
  HTML('
       { background-color: #dec4de;}
       #action4 { width: 275 px; color:red; background-color:yellow }
       #action1, #action2, #action3 { color:black; background-color:lime }
       body, label, input, button, select { font-family: "Arial"; }'
  )
)),    

titlePanel("My Shiny App!"),

sidebarLayout(
  sidebarPanel(
    tabsetPanel(type = "tabs", 
                tabPanel("About", "Developer Info here"), 
                tabPanel("How to Use", "User Docs"))

  ),

  mainPanel(
    img(src="capstone1.jpg", align = "center"),
    br(),br(),
    tags$textarea(id="stext", rows=3, cols=80, "Default value"),
    br(),br(),br(),
    actionButton("action1", "Action 1"),
    actionButton("action2", "Action 2"),
    actionButton("action3", "Action 3"),
    br(), br(),
    actionButton("action4", 
                   label = "High < < < <  PROBABILITY  > > > > Low")
  )
)))

server.R

library(shiny)

shinyServer( function(input, output, session) {

   observeEvent(input$action1, {
      x <- input$stext
      print(input$action2.text)
      updateTextInput(session, "stext", value=paste(x, "Btn 1"))
   })

   observeEvent(input$action2, {
      x <- input$stext
      print(input$action2.label)
      updateTextInput(session, "stext", value=paste(x, "Btn 2"))
   })  

   observeEvent(input$action3, {
      x <- input$stext
      print(x)
      updateTextInput(session, "stext", value=paste(x, "Btn 3"))
   })  

})

UPDATE: Adding code for shinyBS

ui.R

{
   library(shinyBS)
   ....
   bsButton("myBtn", "")
   ...

}

server.R

{
     ....
     updateButton(session, "myBtn", "New Label")
     ....
}
PeterV
  • 195
  • 1
  • 13
  • 1
    When I create that action button and store it as an object and explore, I see that this - ab$children[[1]][[2]] gives you the label name. Can you try if input$action1$children[[1]][[2]] gives you what you need? – Gopala Jan 01 '16 at 00:32
  • Looking at this source code, the icon and label are stored as a list in the shiny tags. So, it makes sense that the label is obtained as the second element of that list. https://github.com/rstudio/shiny/blob/master/R/input-action.R – Gopala Jan 01 '16 at 00:37
  • @user3949008, I got the error "Warning: Unhandled error in observer: $ operator is invalid for atomic vectors observeEvent(input$action1)" – PeterV Jan 01 '16 at 01:05
  • I am also struggling to find a way to change the label of the actionButton. Hopefully the solution here might also be helpful there. – PeterV Jan 01 '16 at 02:49
  • I have switched to shinyBS and used bsButton. Now I can update the label of the button, but still not able to read the label. – PeterV Jan 02 '16 at 16:34

1 Answers1

2

In order to update the label (A), without using shinyBs, in ui.R you can use textOutput(), then to read and catch the click of the user (B), in server.R you can use observeEvent() and updateTextInput() but starting from the initial result of your function.


(A) Updating the label of the buttons :

p(
  actionButton("controller1", label=textOutput("option1")),
  actionButton("controller2", label=textOutput("option2")),
  actionButton("controller3", label=textOutput("option3")),
  style="text-align: center;"),

with in server.R

output$option1 <- renderPrint({
    if(input$userText %in% c("", " ")){cat("waiting")}  ## to avoid bad readings.

    else{cat(myFunction(input$userText)[1])}            ## here use the first element of 
                                                        ## the result of your function.
})

output$option2 <- renderPrint({
    if(input$userText %in% c("", " ")){cat("for")}
    else{cat(myFunction(input$userText)[2])}
})

output$option3 <- renderPrint({
    if(input$userText %in% c("", " ")){cat("you")}
    else{cat(myFunction(input$userText)[3])}
})

(B) To read and catch the click of the user :

shinyServer( function(input, output, session) {

    observeEvent(input$controller1, {
        if(input$userText %in% c("", " ")){x <- "waiting"}    ## to stay consistent
                                                              ## in case the user clicks.
        else{x <- myFunction(input$userText)[1]}

    updateTextInput(session, "userText", value = paste(input$userText, x))
    })

    observeEvent(input$controller2, {
        if(input$userText %in% c("", " ")){x <- "for"}
        else{x <- myFunction(input$userText)[2]}

    updateTextInput(session, "userText", value = paste(input$userText, x))
    })

    observeEvent(input$controller3, {
        if(input$userText %in% c("", " ")){x <- "you"}
        else{x <- myFunction(input$userText)[3]}

    updateTextInput(session, "userText", value = paste(input$userText, x))
    })

})
  • Thanks Gwenael. After looking at your code, I found that I can just create a array object that I can use to store the label when i update the button. So I can get the label by accessing the element in the array at that index. – PeterV Jan 04 '16 at 19:59