Search code examples
rshinyimageurl

ShinyAlert goes wrong with local image (order of execution)


I've run into the following problem

using ShinyAlert * if I add an imageUrl to the message, if it's an internet url address, no problem, the message appears, image appears instantly, and then the other code pieces run, BUT if it is an image on my harddisk, this happens: Message without image opens other code after the message runs THEN the image appears..

I'm clueless why and I've tried it on 3 different computers now, and tested it with js swal approach as well, same problem:

library(shiny)
library(sweetalertR)
library(shinyjs)
library(shinydashboard)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  useShinyalert(),
  textInput("expr", label = "Enter an R expression",
            value = "bio",width="600px"),

  actionButton(inputId ="mybutton", "Run")


)

server <- function(input, output, session) {
  shinyEnv <- environment()

  observeEvent(input$mybutton, {
    print(getwd())
    shinyalert(title = 'hello', imageUrl ="insert http url or local disk path/name.png", imageSize = "80x80")

    for (i in 1:50000){ print(i)  ### see if picture in message shows up before this code runs
      }
    })
  }



shinyApp(ui = ui, server = server)

EXTRA example how I code it with a jscode

swal5 = function(params) { 
      var defaultParams = {
        title : null,
        text : null,
        type : null  
      };
      params = shinyjs.getParams(params, defaultParams);
      swal({title : params.title, text : params.text, type : params.type,
        showCancelButton : true,
        cancelButtonText : "No, cancel please!",
        showConfirmButton : true,
        confirmButtonText : "Yes, merge the files!",
        closeOnCancel : true,
        closeOnConfirm: false},
        evalFunction = function(isConfirm){
          if (isConfirm === true) {
            var val1= 1;
            Shiny.onInputChange("MergeRaw_OP", [val1, Math.random()]);}

        });
    };

Solution

  • Your code (at least the first one, haven't tried the second one) isn't actually right and has a lot of extra code that doesn't do anything. No need to load shinyjs, shinydashboard, to save the environment, and sweetalertR is the wrong package. When I try to run the correct and minimal version of that code chunk, it does work for me.

    library(shinyalert)
    library(shiny)
    ui <- fluidPage(
      useShinyalert(),
      actionButton(inputId ="mybutton", "Run")
    )
    
    server <- function(input, output, session) {
      observeEvent(input$mybutton, {
        shinyalert(title = 'hello', imageUrl ="http://deanattali.com/img/deanimg.jpeg", imageSize = "80x80")
        for (i in 1:50000){ print(i) }
      })
    }
    
    shinyApp(ui = ui, server = server)