如何使用actionButton来控制闪亮项目的值

时间:2018-03-27 15:27:21

标签: r shiny

大家。我想在闪亮中使用两个actionButtons来控制项目的值。代码如下:

ui <- fluidPage(
actionButton(
inputId = "bt2",
label = "BT2",
styleclass = "info"
 ),
 actionButton(
inputId = "bt1",
label = "BT1",
styleclass = "info"
  ),

textOutput(outputId = "test")
)

server <- function(input, output, session) {
test <- eventReactive(eventExpr = input$bt1, {
"1"
})

observeEvent(eventExpr = input$bt2, {
test <- reactive({"2"})
 })

  output$test <- renderText({
test()
})
}

shinyApp(ui = ui, server = server)

然而,它没有用! 有人可以帮帮我吗? 非常感谢你!!!

1 个答案:

答案 0 :(得分:0)

我添加了reactiveValues来帮助您,因为您想使用observeEvent

等不同方法对其进行更改
library(shiny)
ui <- fluidPage(
  actionButton(
    inputId = "bt2",
    label = "BT2",
    styleclass = "info"
  ),
  actionButton(
    inputId = "bt1",
    label = "BT1",
    styleclass = "info"
  ),

  textOutput(outputId = "test")
)

server <- function(input, output, session) {

  v <- reactiveValues()

  observeEvent(input$bt1,{
    v$test <- "1"
  })

  observeEvent(input$bt2, {
    v$test  <- "2"
  })

  output$test <- renderText({
    v$test
  })
}

shinyApp(ui = ui, server = server)