闪亮的 observeEvent 显示第一个事件,但不显示第二个、第三个
Shiny observeEvent shows first event, but not second, third
TL;DR: observeEvent 仅在第一个实例上起作用,但在后续实例上不起作用。
解释:
在下面的一个闪亮的小应用程序中,我根据用户输入动态构建了一个 URL,它指向我在 GH 上托管的相应预渲染“计时器”gif 图像。在下面这个闪亮的应用程序代码中,observeEvent 可以很好地拉取 sliderInput 值(例如 5sec),构建一个 URL,然后单击 'go' 将使用一个聪明的 shinyjs 包显示计时器。但是,如果我不更改输入(仍为 5 秒),则单击开始不会重建图像。如果我将值更改为不同的数字(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)
您的代码有几个问题:
如果您再次按 input$go
(w/o 更改滑块),renderText
将不会重新触发。因为这个想法是 observer/render
在他们的反应 改变 时触发。由于您的 renderText
依赖于 rv$time
,当 input$time
不改变时它也不会改变,渲染函数不会在随后的按钮按下时触发。这可以通过在渲染函数中包含 input$go
设置对按钮的附加依赖性来解决。
- 但是,这不会解决您的问题,因为浏览器使用缓存。它看到
<img>
标签没有改变(相同 src
),因此它不会重新加载图片。要避免这种情况,您可以通过向 src
. 添加时间戳来使用 Disable cache for some images 中的技巧
长话短说,这段代码可以解决问题:
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%">')
})
TL;DR: observeEvent 仅在第一个实例上起作用,但在后续实例上不起作用。
解释: 在下面的一个闪亮的小应用程序中,我根据用户输入动态构建了一个 URL,它指向我在 GH 上托管的相应预渲染“计时器”gif 图像。在下面这个闪亮的应用程序代码中,observeEvent 可以很好地拉取 sliderInput 值(例如 5sec),构建一个 URL,然后单击 'go' 将使用一个聪明的 shinyjs 包显示计时器。但是,如果我不更改输入(仍为 5 秒),则单击开始不会重建图像。如果我将值更改为不同的数字(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
将不会重新触发。因为这个想法是observer/render
在他们的反应 改变 时触发。由于您的renderText
依赖于rv$time
,当input$time
不改变时它也不会改变,渲染函数不会在随后的按钮按下时触发。这可以通过在渲染函数中包含input$go
设置对按钮的附加依赖性来解决。- 但是,这不会解决您的问题,因为浏览器使用缓存。它看到
<img>
标签没有改变(相同src
),因此它不会重新加载图片。要避免这种情况,您可以通过向src
. 添加时间戳来使用 Disable cache for some images 中的技巧
input$go
(w/o 更改滑块),长话短说,这段代码可以解决问题:
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%">')
})