Plotly 不在闪亮的服务器上渲染

Plotly plot not rendering on shiny server

我正在使用 plotly 包来显示闪亮的情节。在我的本地机器上,绘图完美呈现,但是当我 运行 闪亮服务器上的闪亮应用程序时,我收到错误 "Error: cannot open file 'Rplots.pdf'" 应该呈现绘图的地方。我已经尝试使用 dev.off() 命令,因为我已经阅读了一些其他可能的解决方案,这些解决方案将此作为可能的解决方案。下面我在 server.R 脚本中粘贴了用于创建图表的代码:

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

在 ui.R 脚本中,我使用以下命令调用绘图:

plotlyOutput("recSalesPlot", width = "100%", height = 600)

我不能说我了解问题的根源或为什么我的解决方案对我有效,但我 运行 遇到了同样的问题,只是在我的开头添加了 pdf(NULL)脚本和一切似乎工作正常。不需要 dev.off()(添加它会给我带来错误)。

一个jenwen的回答大体上是正确的,但是: 请注意,您应该在 renderPlotly() 内添加 pdf(NULL) 而不是在脚本开头。 而且,如果您开始使用 pdf(NULL) 多次调用 renderPlotly(),它将产生一个 "too many open devices" 错误,这将杀死服务器上的所有图形设备,包括 png、tiff 等,而不仅仅是 pdf。 要解决这个问题 - 在 pdf(NULL) 之前,您可以调用 graphics.off() 来清除当前打开的所有设备,并且一次只有一个。

像这样的错误通常意味着您的目录不属于 shiny 服务器正在 运行 的用户所有。

我建议避免@jenwen 的回答,因为它通过不尝试编写中间文件来规避根本问题,但通常会导致 Error in plot(NULL): too many open devices 用户使用率过高。

一个更好的解决方案是符合 shiny-server 的约定:当将应用程序放入 shiny server 目录时,例如/srv/shiny-server/app-name,我把权限改成已经配置为运行 shiny-server:

的用户
sudo chown -R shiny:shiny /srv/shiny-server/app-name

这样用户就可以毫无问题地写入和删除该应用程序中的临时目录。