2016-01-01 29 views
1

获得被点击的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") 
    .... 
} 
+1

当我创建该动作按钮并将其存储为对象并进行探索时,我发现这个ab $ children [[1]] [[2]]会为您提供标签名称。你可以尝试如果输入$动作1 $子[[1]] [[2]]给你你需要什么? – Gopala

+0

查看此源代码,图标和标签将作为列表存储在闪亮的标签中。因此,标签是作为该列表的第二个元素获得的。 https://github.com/rstudio/shiny/blob/master/R/input-action.R – Gopala

+0

@ user3949008,我得到了错误“警告:观察者中的未处理错误:$运算符对原子向量无效 observeEvent(input $ action1)“ – PeterV

回答

2

为了更新标签(A),而不使用shinyBs,在ui.R可以使用textOutput(),然后阅读和捕捉的点击用户(B),在server.R中,您可以使用observeEvent()updateTextInput(),但是从函数的初始结果开始。


(A)更新按钮的标签:在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)要读取和捕捉点击

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

的用户:

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

}) 
+0

谢谢格温埃尔。看完你的代码后,我发现我可以创建一个数组对象,当我更新按钮时,可以使用它来存储标签。所以我可以通过访问该索引处数组中的元素来获取标签。 – PeterV

相关问题