R 如何在零位停止操作按钮计数器?

R 如何在零位停止操作按钮计数器?,r,shiny,R,Shiny,我有一个闪亮的应用程序,它输出两个动作按钮{+1,-1}的点击总数。有人能告诉我如何使计数器的最小值停在零位吗。例如,如果新会话的前两次单击为Remove1和Remove1,则输出将为0,而不是-2 library(shiny) ui <- fluidPage( actionButton("add", "Add 1"), actionButton("remove", "Remove 1"), te

我有一个闪亮的应用程序,它输出两个动作按钮{+1,-1}的点击总数。有人能告诉我如何使计数器的最小值停在零位吗。例如,如果新会话的前两次单击为Remove1和Remove1,则输出将为0,而不是-2

library(shiny)

ui <- fluidPage(
  actionButton("add", "Add 1"),
  actionButton("remove", "Remove 1"),
  textOutput("counter")
)

server <- function(input, output, session) {
  output$counter <- renderText(
    input$add + input$remove * -1
  )
}

shinyApp(ui, server)
库(闪亮)

ui您似乎无法更改
操作按钮的值

相反,您可以像这样使用
reactiveVal()

server <- function(input, output, session) {
  total <- reactiveVal( 0 )
  observeEvent( input$add, total( total() + 1 ))
  observeEvent( input$remove, total( max( 0, total() - 1 )))
  
  output$counter <- renderText( total() )
}

server Try
max(input$add+input$remove*-1,0)
即使在再次单击remove之后,也会显示一个值0,但需要单击add两次才能转到1