在有光泽的应用程序中捕获actionButton的标签 [英] Capture label of actionButton in Shiny app

查看:159
本文介绍了在有光泽的应用程序中捕获actionButton的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取被点击的 actionButton 标签的最佳方法是什么?我有一个 actionButton ,其中的标签已更新.当用户单击时,我需要捕获它.我尝试了input $ action2.label和input $ action2.text,但都没有.

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

})

更新:为ShinyBS添加代码

UPDATE: Adding code for shinyBS

ui.R

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

}

server.R

{
     ....
     updateButton(session, "myBtn", "New Label")
     ....
}

推荐答案

为了更新标签(A),而无需使用ShinyB,可以在ui.R中使用textOutput(),然后在server.R中读取并捕获用户(B)的点击,您可以使用observeEvent()updateTextInput(),但要从函数的初始结果开始.

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)更新按钮标签:

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

with in server.R

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)阅读并捕获用户的点击:

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

})

这篇关于在有光泽的应用程序中捕获actionButton的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆