daattali / shinycssloaders

⌛ Add loading animations to a Shiny output while it's recalculating
https://daattali.com/shiny/shinycssloaders-demo/
Other
395 stars 45 forks source link

Set up custom image path #57

Closed learning-freak closed 3 years ago

learning-freak commented 3 years ago

Hello, let assume we have an image named mypic.gif in a folder named myfolder. how to set up image path with image parameter in oder to use custom image ? withSpinner(plotlyOutput("myplot"), image = "how to setup up path to custom image ?")

I tried : withSpinner(plotlyOutput("myplot"), image = "C:/Users/john/Documents/myproject/mypic.gif") but its doesnt work. Some help would be appreciated

daattali commented 3 years ago

The image is loaded using a regular HTML img tag. You need to work out the pathing so that you can load the image is shiny as a regular img tag. If it works in img, it'll work here too.

learning-freak commented 3 years ago

I found the solution, we have just to put image tag inside list object. Here the full working code :

library(shiny)
library(shinycssloaders)

ui <- fluidPage(

    # Application title
    titlePanel("example"),

    mainPanel(
        shiny::tags$img(src = "mygif.gif", height = 100, width = 100), #displayed perfectly on app

        shinycssloaders::withSpinner(plotOutput("distPlot2"), 
                                     image = list(src = "mygif.gif"))
    )
)

server <- function(input, output) {

    output$distPlot2 <- renderPlot({
        hist(rnorm(1000), main = "with my custom gif")
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Thank you

daattali commented 3 years ago

That's strange, you shouldn't have to use list(src=). You should just be able to use image = path.

If the image is located in a www folder or if it's used together with addResourcePath() it should just work

learning-freak commented 3 years ago

I put the image in a www folder and i used list(src=) and its works like charm

daattali commented 3 years ago

It should work without using list src. If you have a few minutes could you upload a ZIP of a directory that contains the app along with the image file with your directory structure, where it isn't working?

learning-freak commented 3 years ago

Please see attachement. Its work when i use list scr with www folder but not when i set up like this image = C:/Users/John/Desktop/example/www/mygif.gif or when i want to use another image file in a specific folder e.g C:/Users/John/Desktop/photos/myphoto.png example.zip

daattali commented 3 years ago

Your example works for me using image = "mygif.gif". To be clear, you also need to ensure the working directory is set correctly, which means:

learning-freak commented 3 years ago

Okay, i didn't notice that. Thank you