【问题标题】:R Shiny alternate actionButtonR闪亮的交替动作按钮
【发布时间】:2021-07-23 21:58:10
【问题描述】:

我有一个简单的app,每次点击plus1,x值加1,每次点击plus10,x值加10。但是我想plus10只能是有效的点击plus1 后。而plus1被点击后,plus10只能被点击一次才能再次失效,这需要再次点击plus1才能生效。所以基本上我希望用户交替点击这两个按钮,在启动应用程序时需要先点击plus1。理想情况下,如果用户尝试点击plus10 而不先点击plus1,则会向用户发出警告:“请先点击 plus1”。

library(shiny)

ui <- fluidPage(
  actionButton("plus1", "+ 1"),
  actionButton("plus10", "+ 10"),
  textOutput("value")
)

server <- function(input, output, session) {
  x = reactiveVal(0)
  
  observeEvent(input$plus1,{
    x(x()+1) # increment x by 1
  })
  
  observeEvent(input$plus10,{
    x(x()+10) # increment x by 1
  })
  
  output$value = renderText(x())
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是你想要的,但你为什么想要这个?

    library(shiny)
    
    ui <- fluidPage(
        actionButton("plus1", "+ 1"),
        actionButton("plus10", "+ 10"),
        textOutput("value"),
        textOutput("warning")
    )
    
    server <- function(input, output, session) {
        x = reactiveValues(x=0,click1=TRUE)
        
        observeEvent(input$plus1,{
            if (x$click1) {
                x$x <- x$x+1 # increment x by 1
                x$click1 <- !x$click1
            } else {
                output$warning <- renderText({"Please click plus10 first"})
            }
            
        })
        
        observeEvent(input$plus10,{
            if (!x$click1) {
                x$x <- x$x+10 # increment x by 10
                x$click1 <- !x$click1
            } else {
                output$warning <- renderText({"Please click plus1 first"})
            }
        })
        
        output$value = renderText(x$x)
    }
    
    shinyApp(ui, server)
    

    如果需要解释请告诉我

    【讨论】:

    • 这只是我正在尝试做的简化版本,但以这种方式更容易解决问题。我在应用程序中有两个按钮,一个叫做“批准”,另一个是“下一页”。如果用户没有批准文档,我不希望用户继续到下一页。
    • 很好的答案,我不知道你可以使用这样的反应值。
    【解决方案2】:

    使用shinyjs

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(),
      actionButton("plus1", "+ 1"),
      disabled(actionButton("plus10", "+ 10")),
      textOutput("value")
    )
    
    server <- function(input, output, session) {
      x = reactiveVal(0)
      
      observeEvent(input$plus1,{
        x(x() +1 )
        disable('plus1')
        enable('plus10')
      })
      
      observeEvent(input$plus10, {
        x(x() + 10)
        enable('plus1')
        disable('plus10')
      })
      
      output$value <- renderText({x()})
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 喜欢这个包!包中还有其他有用的功能,谢谢!
    【解决方案3】:

    您可以只使用另一个响应值来跟踪用户是否可以添加 10

    library(shiny)
    
    ui <- fluidPage(
      actionButton("plus1", "+ 1"),
      actionButton("plus10", "+ 10"),
      textOutput("value")
    )
    
    server <- function(input, output, session) {
      x <- reactiveVal(0)
      canPlus10 <- reactiveVal(FALSE)
      
      observeEvent(input$plus1,{
        x(x()+1) # increment x by 1
        canPlus10(TRUE)
      })
      
      observeEvent(input$plus10,{
        if (canPlus10()) {
          x(x()+10) # increment x by 10
        } else {
          showNotification("Please click +1 first")
        }
        canPlus10(FALSE)
      })
      
      output$value = renderText(x())
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这是一个漂亮的答案,可以很容易地扩展到更复杂的应用程序有这个问题。您的回答,但是用户可以多次单击 plus1 按钮,而我的意图是用户只能单击一次。但是使用您的逻辑,我设法创建了另一个名为 canPlus1 = reactiveVal(TRUE) 的反应值,并在 if else 语句中使用了一些逻辑来使其工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多