8

I am using the plotly package to display a plot in shiny. On my local machine the plot renders perfectly, however when I run the shiny app on shiny server I receive the error "Error: cannot open file 'Rplots.pdf'" where the plot is supposed to be rendered. I have tried using the dev.off() command as I had read some other possible solutions that referenced this as a possible solution. Below I pasted my code for creating the graph in the server.R script:

    output$recSalesPlot <- renderPlotly({
       BWplot_rec <- ggplot(d1, aes_string(x = "End_of_Week", y = input$metric_rec))
            BWplot_rec <- BWplot_rec + geom_line(aes(color = Group), size = .25)
            BWplot_rec <- BWplot_rec + geom_point(aes(color = Group), size = 2)
            BWplot_rec <- BWplot_rec + xlab("Week")
            if(input$metric_rec == "NetSales"){
              BWplot_rec <- BWplot_rec + ylab("Euros")
            }
            BWplot_rec <- BWplot_rec + ggtitle(paste0("Average ", input$metric_rec, " Per Group Per Week"))
            BWplot_rec <- BWplot_rec + guides(color=FALSE)
            BWplot_rec <- BWplot_rec + theme(panel.grid.major.y = element_blank(),
                                             panel.grid.minor.y = element_blank())
            p <- ggplotly(BWplot_rec)
            p
          })
}

In the ui.R script I am using the following command to call the plot:

plotlyOutput("recSalesPlot", width = "100%", height = 600)
mikew
  • 347
  • 6
  • 12
  • have you compare the sessionInfo of your local R with the shiny server? To make sure you have all the packages you need on the shiny server. – MLavoie Apr 22 '16 at 17:28
  • I have not done that yet, I will do that and see what the case is. I did read one possible reason is that plotly is trying to write a temp file but does not have permission when accessed from the server but it does from my local, which may be why it works on my local but not the server. – mikew Apr 22 '16 at 17:55

3 Answers3

13

I can't say I understand the root of the issue or why my solution even works for me, but I ran into the same problem and simply added pdf(NULL) at the beginning of my script and everything seems to work just fine. No dev.off() needed (adding it in threw an error for me).

jenwen
  • 246
  • 3
  • 5
  • Jenwen, Thank you! This solution works for me as well. It seems that it stops R from creating these temporary PDFs which were causing the error initially. – mikew May 19 '16 at 14:16
  • 3
    Where exactly do you put `pdf(NULL)`? At the top of `ui.R`, at the beginning of your plotting function, or something else? – landau Feb 03 '17 at 14:07
6

An error like this usually means that your directory is not owned by the user that shiny server is being run by.

I suggest avoiding @jenwen's answer because it circumvents the root issue by not attempting to write an intermediate file, but will often result in a Error in plot(NULL): too many open devices with heavy user usage.

A better solution is to conform to the conventions of shiny-server: when putting the app into the shiny server directory, e.g. /srv/shiny-server/app-name, I change the permissions to the user that has been configured to run shiny-server:

sudo chown -R shiny:shiny /srv/shiny-server/app-name

That way the user can write and delete to temporary directories within that app without issue.

mlegge
  • 6,763
  • 3
  • 40
  • 67
2

A jenwen answer is in general correct but: Please notice that you should add pdf(NULL) inside renderPlotly() not at begin of script. And also if you start to call renderPlotly() with pdf(NULL) more times it will create a "too many open devices" error which will kill all your graphics devices on server inluding png, tiff etc. not only pdf. To solve it - just before pdf(NULL) you can call graphics.off() to clear all devices currently open and have just one at a time.