问题
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")
....
}
回答1:
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))
})
})
来源:https://stackoverflow.com/questions/34552672/capture-label-of-actionbutton-in-shiny-app