在Shiny应用程序中捕获actionButton的标签

时间:2016-01-01 00:26:28

标签: r shiny shinybs

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

ui.R

    #d=69+12*log(2)*(f/440)
    #f=2^((d-69)/12)*440
    #d is midi, f is frequency

    import math
    f=raw_input("Type the frequency to be converted to midi: ")
    d=69+(12*math.log(int(f)/440))/(math.log(2))
    print d`

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

更新:为shinyBS添加代码

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

})

server.R

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

}

1 个答案:

答案 0 :(得分:2)

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


(A)更新按钮的标签:

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

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

})