简体   繁体   English

在R Shiny中连续多次更新UI

[英]Update UI multiple times in a row in R Shiny

I'm trying to make a button blink (cycle through colors) a number of times before finally stopping. 我想在最后停止之前多次使按钮闪烁(循环显示颜色)。 Sort of to simulate a lottery draw. 有点模拟彩票抽奖。

The final color in the end will be determined by a calculation, but first I need to figure out how to make it blink the initial n times before it stops. 最终的最终颜色将由计算确定,但首先我需要弄清楚如何使它在停止之前使初始值闪烁n次。

This is what I tried to make, but it only updates the colors a part of the time, like 2 or 3 times. 这是我试图制作的,但它只是在一部分时间内更新颜色,比如2或3次。

library(shiny)
ui  <- fluidPage(

  uiOutput('ColorButton'),
  actionButton(inputId = 'Generator', label = 'Show colors', style = "background-color: #fff; color: #FF0000; border-color: #FF0000; 
               border-width: 2px; font-size: 20px; font-weight: bolder; 
               border-radius: 6px; height: 60px; display: block; margin-top: 100px; margin-left: auto; margin-right: auto"
  )

)

server <- function(input, output, session) { 
  values <- reactiveValues(go = 0)
  values$color <- '#FF0000'
  observe({ values$style = paste("background-color:", values$color, ";height:300px; width: 300px; border-radius: 150px;
                       display: block; margin-top: 100px; margin-left: auto; margin-right: auto") })

  colors <- c("darkgray", "blue", "red", "green", "orange", "darkblue", "yellow", "gray20", "purple",  "black", "cyan", "violet", "beige", "magenta", "pink", "brown")

  observeEvent(input$Generator, { values$go <- 1 }) 

  observeEvent(values$go, { 
    if(values$go > 0 & values$go < 20) { 

      sampled <- sample(c(1:12), 1)
      values$color <- colors[sampled]
      values$go <- values$go +1
      Sys.sleep(0.1)


   }
  })
  output$ColorButton  <- renderUI({ actionButton(inputId = 'ColorButton', label = NULL, style = values$style)})
}

shinyApp(ui = ui, server = server)

I slightly modified your example and included invalidateLater and isolate in an observer which changes the color and in another observer I handle the case when values$go is 0, so the ColorButton has an initial color. 我略微修改了你的例子并包含了invalidateLater并且在一个观察者中isolate了一个改变颜色的观察者,而在另一个观察者中我处理values$go的情况是0,所以ColorButton有一个初始颜色。

library(shiny)

ui  <- {fluidPage(
  uiOutput('ColorButton'),
  actionButton(inputId = 'Generator', label = 'Show colors', style = "background-color: #fff; color: #FF0000; border-color: #FF0000; 
               border-width: 2px; font-size: 20px; font-weight: bolder; 
               border-radius: 6px; height: 60px; display: block; margin-top: 100px; margin-left: auto; margin-right: auto"
  )
)}

colors <- c("darkgray", "blue", "red", "green", "orange", "darkblue", "yellow", "gray20",
            "purple",  "black", "cyan", "violet", "beige", "magenta", "pink", "brown")

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

  values <- reactiveValues(go = 0)

  observe({
    if (values$go == 0) {
      values$color <- '#FF0000'
      values$go <- values$go +1
    }
  })

  observeEvent(input$Generator, {
    if (values$go == 20) {
      values$color <- '#FF0000'
      values$go <- 0
    }
  })

  observe({
    req(input$Generator)
    invalidateLater(500, session)
    isolate({
      if (values$go > 0 & values$go < 20) {
        sampled <- sample(c(1:12), 1)
        values$color <- colors[sampled]
        values$go <- values$go +1
      }      
    })
  }) 

  observe({ 
    values$style = paste("background-color:", values$color, ";height:300px; width: 300px; border-radius: 150px;
                         display: block; margin-top: 100px; margin-left: auto; margin-right: auto") 
  })

  output$ColorButton  <- renderUI({
    actionButton(inputId = 'ColorButton', label = NULL, style = values$style)
  })
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM