【发布时间】:2016-01-01 00:26:28
【问题描述】:
获取被点击的 actionButton 标签的最佳方法是什么?我有一个标签已更新的 actionButton。当用户点击时,我需要捕捉它。我尝试了 input$action2.label 和 input$action2.text,但都没有成功。
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]] 为您提供了标签名称。如果 input$action1$children[[1]][[2]] 能满足你的需要,你能试试吗?
-
查看这个源代码,图标和标签作为一个列表存储在闪亮的标签中。因此,将标签作为该列表的第二个元素获得是有道理的。 github.com/rstudio/shiny/blob/master/R/input-action.R
-
@user3949008,我收到错误“警告:观察者中未处理的错误:$ 运算符对于原子向量观察事件(输入$action1)无效”
-
我也在努力寻找改变actionButton标签的方法。希望这里的解决方案在那里也有帮助。
-
我已经切换到 shinyBS 并使用了 bsButton。现在我可以更新按钮的标签,但仍然无法读取标签。