Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly plot not rendering on shiny server

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)
like image 641
mikew Avatar asked Apr 21 '16 18:04

mikew


3 Answers

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).

like image 59
jenwen Avatar answered Nov 19 '22 06:11

jenwen


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.

like image 6
mlegge Avatar answered Nov 19 '22 05:11

mlegge


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.

like image 2
Darek Bienkowski Avatar answered Nov 19 '22 06:11

Darek Bienkowski