获得被点击的actionButton的标签的最佳方式是什么?我有一个actionButton标签已更新。当用户点击时,我需要捕获它。我尝试了输入$ action2.label并输入$ action2.text,但都没有工作。在Shiny应用程序中捕获actionButton的标签
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"))
})
})
更新:增加对shinyBS代码
ui.R
{
library(shinyBS)
....
bsButton("myBtn", "")
...
}
server.R
{
....
updateButton(session, "myBtn", "New Label")
....
}
当我创建该动作按钮并将其存储为对象并进行探索时,我发现这个ab $ children [[1]] [[2]]会为您提供标签名称。你可以尝试如果输入$动作1 $子[[1]] [[2]]给你你需要什么? – Gopala
查看此源代码,图标和标签将作为列表存储在闪亮的标签中。因此,标签是作为该列表的第二个元素获得的。 https://github.com/rstudio/shiny/blob/master/R/input-action.R – Gopala
@ user3949008,我得到了错误“警告:观察者中的未处理错误:$运算符对原子向量无效 observeEvent(input $ action1)“ – PeterV