1

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

用户界面

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

服务器.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 添加代码

用户界面

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

}

服务器.R

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

1 回答 1

2

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


(A) 更新按钮的标签:

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

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

})
于 2016-01-04T18:33:09.897 回答