代码之家  ›  专栏  ›  技术社区  ›  K.Hua

R使用反应值亮起一个动作按钮两次

  •  0
  • K.Hua  · 技术社区  · 7 年前

    我有以下闪亮的应用程序

    server <- function(input, output, session) {
      rv <- reactiveValues(i = 0)
    
      output$myplot <- renderPlotly({
        dt = data.frame(x = 1:10, y = rep(rv$i,10))
        plot_ly(dt, x = ~x, y =~y) 
      })
    
      observeEvent(input$run,{
        rv$i <- 0
        observe({
          isolate({rv$i = rv$i + 1})
          if (rv$i < 10){invalidateLater(1000, session)}
        })
      })
    }
    
    ui <- fluidPage(
      actionButton("run", "START"),
      plotlyOutput("myplot")
    )
    
    shinyApp(ui = ui, server = server)
    

    action按钮只工作一次:如果我点击它,绘图就会更新。但问题是我不能点击两次,因为它会导致应用程序崩溃。

    我希望这样,每次我点击action按钮,rv$的值就会回到0,动画会重新开始。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Geovany    7 年前

    library(shiny)
    library(plotly)
    
    server <- function(input, output, session) {
      rv <- reactiveValues(i = 0)
    
      output$myplot <- renderPlotly({
        dt = data.frame(x = 1:10, y = rep(rv$i,10))
        plot_ly(dt, x = ~x, y =~y, mode = "markers", type = 'scatter') 
      })
    
      observeEvent(input$run,{
        rv$i <- 0
      })
    
      observe({
        isolate({rv$i = rv$i + 1})
        if (rv$i < 10){invalidateLater(1000, session)}
      })
    
    }
    
    ui <- fluidPage(
      actionButton("run", "START"),
      plotlyOutput("myplot")
    )
    
    shinyApp(ui = ui, server = server)
    
    推荐文章