本文介绍了在Chrome中重新加载动画GIF时出现问题-第二部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续Mark的建议此处我现在正在使用uiOutputrenderUI.

Continuing from Mark's suggestion here I am now using uiOutput and renderUI.

下面的代码显示动画gif("anione").按下再次获得"按钮后,它会以我无法完全理解的方式使用Javascript重置Chrome,然后通过用另一个动画gif("anitwo")覆盖原始动画gif来模拟我的大型程序.

The code below displays an animated gif ("anione"). When the "Again" button is pressed it resets Chrome with Javascript in ways that I don't fully understand and then simulates my larger program by overwriting the original animated gif with another animated gif ("anitwo").

但是,不显示"anitwo",而是显示"anione".我可以查看本地文件,并查看该文件是否已写入并通过在Chrome中打开它来成功查看.如何查看同名的其他文件?

However, "anitwo" is not displayed, "anione" is displayed. I can view the local file and see that it was written and view it successfully by opening it in Chrome. How do I view a different file of the same name?

我意识到我对"www/tmp/ani.gif"和"tmp/ani.gif"的使用不一致,并且了解到"www"中的文件看起来很闪亮.但是,没有这些不一致,我会得到找不到文件的错误.

I realize that I am inconsistent in my use of "www/tmp/ani.gif" and "tmp/ani.gif" and understand that shiny looks for files in "www". Yet, without these inconsistencies I get file not found errors.

ani.gif和ani2.gif是此处

ani.gif and ani2.gif are 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)  

这篇关于在Chrome中重新加载动画GIF时出现问题-第二部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:32