1

I am trying to update divs while in a loop, some of which contain images. Using removeUI(..., immediate = TRUE) I can remove them and then replace them by new divs, with insertUI(..., immediate = TRUE). Although the texts appear in real time, the images do not load until we are out of the loop (see example below, you don't even have to load an image, a question mark will appear after the loop ends).

In my browser I can see the img tags are created in HTML, but still no images appear live.

Here is a reproducible example:

ui <- fluidPage(
  actionButton("add","")
)
server <- function(input, output, session) {
  for(i in 1:3){
    Sys.sleep(1.5)
    insertUI(
      selector = "#add",
      where = "afterEnd",
      ui = div(style = paste0("width: 75px; height: 75px; background-color: white;"), h5("Text appears live", align = "center"), 
               div(h6("Text inside a div appears live")),
               div(id = "img", img(src = "image.jpg", alt = "Images do not appear live")
               )
      ),
      immediate = TRUE
    )
  }
}
shinyApp(ui, server)

Is this normal behavior for shiny? If so is their a way to bypass it and to see the images appear directly? Another way to do it?

3
  • Your script is working fine. Are you running the app in the browser or in RStudio? Try to run it external in chrome or firefox. Jul 5, 2019 at 20:44
  • I already tried running it in RStudio and Chrome and it still does not work but after your suggestion I tried Firefox and the images appear fine at the same time as the text... This is a bit strange didn't you have any problem in RStudio? Jul 5, 2019 at 22:20
  • Actually it does not even work in Firefox the alt text gets printed live along with the text but when I attach real images they only appear after the loop is finished just like Chrome and RStudio Jul 5, 2019 at 22:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.