Strange bug when You use Plotly on Your own server

I used my Shiny apps only locally and only with shinyapps.io in the past, but now I’ve got a VPS. When I migrated my previous apps to the VPS I found a strange bug. When there was ggplot based Plotly plot in my code I got an error message: “An error has occurred. Check your logs or contact the app author for clarification.”.

I checked the log file which said:

Warning: Error in : cannot open file 'Rplots.pdf'
Stack trace (innermost first):
    88: 
    87: grid.Call
    86: convertUnit
    85: convert
    84: unitConvert
    83: %||%
    82: gg2list
    81: ggplotly.ggplot
    80: ggplotly
    79: ggplotly [/srv/shiny-server/survey_from_gf/server.R#136]
    78: func
    77: origRenderFunc
    76: output$plotlyBar
     1: runApp

Fortunately I found this blog post which says I just only need to paste pdf(NULL) to my code which solves the problem.

So if You have a same issue You just only need to paste it to the start of the renderPlotly function like this:

shinyServer(
  function(input, output) {
     output$plotlyPlot <- renderPlotly({
        pdf(NULL)
 
        ggplot(mtcars, aes(factor(cyl))) +
        geom_bar()
 
        ggplotly(p)
     })
})

Write me if You know a more elegant way to fix this bug.

One thought on “Strange bug when You use Plotly on Your own server

Leave a Reply

Your email address will not be published. Required fields are marked *