r - Shiny 的observeEvent显示第一个事件,但不是第二个,第三个

标签 r shiny tidyverse reprex

TL;DR: observeEvent 仅在第一个实例上起作用,但不在后续实例上。
解释:
在下面的一个 Shiny 的小应用程序中,我根据用户输入动态构建了一个 URL,该 URL 指向我在 GH 上托管的相应的预渲染“计时器”gif 图像。在下面这个 Shiny 的应用程序代码中,observeEvent 可以很好地提取 slider 输入值(例如 5 秒),构建一个 URL,然后单击“开始”将使用一个聪明的 shinyjs 包显示计时器。但是,如果我不更改输入(仍然是 5 秒),单击 go 不会重建图像。如果我将值更改为不同的数字(4 秒),它将显示正确的 gif。如果我改回原始数字(5 秒),则没有 gif。如果我转到一个新号码(3 秒),请更正 gif。如果我在 observeEvent 中打印 input$time 或 rv$time 的值,那么这些值中的每一个都会正确更新(都打印相应的值)。
目标:在每次更新 input$go 时显示对应于 input$time 的 gif
代表:

library(shiny)
library(tidyverse)
library(glue)
library(shinyjs)

# Define UI
ui <-  navbarPage(title = "Reprex", 
                  ## RHYME TIME -----
                  tabPanel("Time",
                           useShinyjs(),
                           sidebarLayout(
                             sidebarPanel( 
                               sliderInput("time",
                                           "Seconds",
                                           min = 3,
                                           max = 10,
                                           value = 5
                               ),
                               actionButton(inputId = "go",
                                            label = "Go"), 
                             ),
                             mainPanel(
                               HTML("<center>"),
                               shinyjs::hidden(htmlOutput("simple_timer")),
                               HTML("</center>")
                               
                             )
                           )
                  )
)

# Define server logic
server <- function(input, output, session) {
  #a container to store my time var
  rv <- reactiveValues(
    time = 0
  )
  
  #the event that is triggered by input$go getting clicked
  observeEvent(input$go, {
    rv$time <- input$time #this should update rv$time after go is clicked
    shinyjs::show("simple_timer") #this is the clever package with simple show/hide funs
  })
  
  #the reactive text that generates my HTML
  output$simple_timer <- renderText({
    glue::glue('<img src ="https://github.com/matthewhirschey/time_timer/raw/main/data/{rv$time}_sec.gif", 
            align = "center", 
            height="50%", 
            width="50%">')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

最佳答案

您的代码有几个问题:

  • renderText如果按 input$go 将不会重新触发再次(不改变 slider )。因为这个想法是observer/render每当他们的 react 时触发 更改 .作为您的renderText取决于 rv$time input$time 时不会改变不会改变,渲染功能不会在随后的按钮按下时触发。这可以通过包含 input$go 来解决。在渲染函数中设置对按钮的附加依赖。
  • 但是,这不会解决您的问题,因为浏览器使用缓存。它看到 <img>标签没有改变(相同的 src ),因此它不会重新加载图片。为了避免这种情况,您可以使用 Disable cache for some images 中的技巧通过将时间戳添加到 src .

  • 长话短说,这段代码可以解决问题:
    output$simple_timer <- renderText({
       input$go # make code dependent on the button
       # add `?timestamp=<timestamp>`to the src URL to convince the browser to reload the pic
       glue::glue('<img src ="https://github.com/matthewhirschey/time_timer/raw/main/data/{rv$time}_sec.gif?timestamp={Sys.time()}", 
                  align = "center", 
                  height="50%", 
                  width="50%">')
    })
    

    关于r - Shiny 的observeEvent显示第一个事件,但不是第二个,第三个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66762158/

    相关文章:

    r - R 中按字符串格式过滤

    r - 在r中使用atop函数时获得恒定的文本大小

    R 错误 - 无法更改 'df' 的锁定绑定(bind)值

    r - XTS 尺寸限制

    r - 删除值频率小于 R 中的 x 的行

    R Shiny - 如何在渲染函数之间共享变量?

    R Shiny : checkBox format

    r - 根据经纬度获取 K 个最近邻

    r - 如何正确为 r 代码创建 exe

    r - cols 在 Shiny 应用程序的 Tags$textarea 中不起作用