R 闪亮的服务器,无法插入openxlsx绘图
使用runApp()从控制台运行以下闪亮的应用程序时,它会按预期工作,显示绘图,并提供下载嵌入绘图的excel文件的功能。在闪亮服务器上运行相同的应用程序会产生错误 无法打开文件“Rplots.pdf”R 闪亮的服务器,无法插入openxlsx绘图,r,shiny,shiny-server,R,Shiny,Shiny Server,使用runApp()从控制台运行以下闪亮的应用程序时,它会按预期工作,显示绘图,并提供下载嵌入绘图的excel文件的功能。在闪亮服务器上运行相同的应用程序会产生错误 无法打开文件“Rplots.pdf” 闪亮服务器v1.4.2.786 Node.js v0.10.40 R版本3.3.1(2016-06-21)-“头发上的虫子” Ubuntu 14.04.4 LTS library(shiny) library(openxlsx) library(magrittr) # Define UI f
- 闪亮服务器v1.4.2.786
- Node.js v0.10.40
- R版本3.3.1(2016-06-21)-“头发上的虫子”
- Ubuntu 14.04.4 LTS
library(shiny) library(openxlsx) library(magrittr) # Define UI for application that draws a histogram ui <- shinyUI(fluidPage( # Application title titlePanel("Old Faithful Geyser Data"), # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel( sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30), downloadButton('specDataDownload', label = "Download", class = NULL) ), # Show a plot of the generated distribution mainPanel( plotOutput("distPlot") ) ) )) # Define server logic required to draw a histogram server <- shinyServer(function(input, output) { output$distPlot <- renderPlot({ # generate bins based on input$bins from ui.R x <- isolate({faithful[, 2] }) bins <- seq(min(x), max(x), length.out = input$bins + 1) # draw the histogram with the specified number of bins hist(x, breaks = bins, col = 'darkgray', border = 'white') }) output$specDataDownload <- downloadHandler( filename = function() { paste("ProcessedPlateAssay", gsub("-|[[:space:]]|:", "", Sys.time()), ".xlsx", sep = "_") }, content = function(con) { x <- isolate({faithful[, 2] }) bins <- seq(min(x), max(x), length.out = input$bins + 1) output <- createWorkbook() addWorksheet( output, "One") hist( x, breaks = bins, col = 'darkgray', border = 'white') insertPlot( output, sheet = 1, startRow = (1), startCol = 5, width = 6.5, height = 3, fileType = "png", units = "in", dpi = 600) saveWorkbook( wb = output, file = con ) }) }) # Run the application shinyApp(ui = ui, server = server)
库(闪亮) 库(openxlsx) 图书馆(magrittr) #为绘制直方图的应用程序定义UI
ui我想提供一个简单的例子,因为我一直在努力解决
的openxlsx
函数在闪亮服务器中不工作的问题,可能是由于insertPlot
或tempfile
的权限问题。对我来说最有效的方法是使用dev.copy
,因为它可以非常灵活地处理分辨率,并且还有一个缩放参数,可以调整图像元素的大小,使其更适合演示。我还包括了使用ggsave
函数的第二种方法 下面是一个可复制的示例,部分来自于中@sebkopf的答案png
库(闪亮) 库(openxlsx) 图书馆(GG2)
ui遇到了同样的问题。我们是这样解决的:(1)使用jpeg打开(临时)绘图;(2) 执行plot命令,(3)关闭jpeg,使用insertImage。嗨,我最终也解决了这个问题,但根本问题是我服务器上的闪亮用户没有对文件夹的写入权限(它被创建为git存储库并推送到服务器)。请记住检查Shinny是否是您试图写入的目录的所有者:通过sudo chown shinny:shinny/var/shinny server/www/shinny_test/work
library(shiny) library(openxlsx) library(ggplot2) ui <- fluidPage( downloadLink("downloadExcel", "Excel Graph Download") ) server <- function(input, output) { p1 <- qplot(mpg, data=mtcars, geom="density", fill=as.factor(gear), alpha=I(.5), main="Distribution of Gas Mileage") p2 <- qplot(age, circumference, data = Orange, geom = c("point", "line"), colour = Tree) output$downloadExcel <- downloadHandler( filename = "savePlot.xlsx", content = function(file){ wb <- createWorkbook() addWorksheet(wb, "ggsave", gridLines = F) addWorksheet(wb, "png", gridLines = F) # Method 1: using ggsave ggsave("p1.png", plot = p1, scale = .6) # Scale parameter resizes the object making text more legible ggsave("p2.png", plot = p2, scale = .6) insertImage(wb, "ggsave", "p1.png", width = 5, height = 3.5) insertImage(wb, "ggsave", "p22.png", startCol = "J", width = 16, height = 10, units = "cm") # Method 2: using png function png("p11.png") print(p1) dev.off() insertImage(wb, "png", "p11.png", width = 5, height = 3.5) png("p22.png") print(p2) dev.off() insertImage(wb, "png", "p22.png", startCol = "J", width = 16, height = 10, units = "cm") saveWorkbook(wb, file, overwrite = T) unlink(c("p1", "p2", "p11", "p22")) # To remove the images from the server } ) } shinyApp(ui, server)