gpt4 book ai didi

javascript - 在 Chrome 中重新加载动画 GIF 时出现问题 - 第二部分

转载 作者:行者123 更新时间:2023-12-02 23:56:03 24 4
gpt4 key购买 nike

继续马克的建议here我现在使用 uiOutputrenderUI

下面的代码显示一个 gif 动画(“anione”)。当按下“再次”按钮时,它会以我不完全理解的方式使用 Javascript 重置 Chrome,然后通过用另一个动画 gif(“anitwo”)覆盖原始动画 gif 来模拟我的较大程序。

但是,不显示“anitwo”,而是显示“anione”。我可以查看本地文件并看到它已写入并通过在 Chrome 中打开它来成功查看它。如何查看同名的不同文件?

我意识到我对“www/tmp/ani.gif”和“tmp/ani.gif”的使用不一致,并且理解 Shiny 的查找“www”中的文件。然而,如果没有这些不一致,我会收到文件未找到错误。

ani.gif 和 ani2.gif 是 here .

library(shiny)
library(shinyjs)
library(magick)

jsCode <- '
shinyjs.reset_anim = function() {
var div_elem = document.getElementById("anim_plot");
var img_elem = div_elem.getElementsByTagName("img")[0];
var src_value = img_elem.getAttribute("src");
img_elem.setAttribute("src", "");
img_elem.setAttribute("src", src_value);
}
'


# Define UI ----
ui <- fluidPage(useShinyjs(),
extendShinyjs(text = jsCode),
uiOutput('anim_plot'),
fluidRow(
column(3,
actionButton("do_again", "Again")
)
)
)

# Define server logic ----
server <- function(input, output) {


output$anim_plot <- renderUI({
img(src = 'tmp/ani.gif',
width = '900')
})

observeEvent(input$do_again, {
print("Again")
js$reset_anim()

# When Again clicked, simulate the creation of a new animated gif by reading in a file and
# over writing the old one. Try displaying this new animated gif.

img2 <- image_read("www/tmp/ani2.gif")
image_write(img2, path = "www/tmp/ani.gif")

output$anim_plot <- renderUI({
img(src = 'tmp/ani.gif',
width = '900')
})
})
}

shinyApp(ui = ui, server = server)

最佳答案

你不能简单地做

library(shiny)

# Define UI ----
ui <- fluidPage(
uiOutput('anim_plot'),
fluidRow(
column(3,
actionButton("do_again", "Again")
)
)
)

# Define server logic ----
server <- function(input, output) {

gif <- reactiveVal("ani.gif")

observeEvent(input$do_again, {
gif("ani2.gif")
})

output$anim_plot <- renderUI({
img(src = gif(), width = "256")
})

}

shinyApp(ui = ui, server = server)

enter image description here

关于javascript - 在 Chrome 中重新加载动画 GIF 时出现问题 - 第二部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55388079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com