当使用runApp()从控制台运行以下闪亮的应用程序时,它按预期工作,显示绘图并提供下载嵌入了绘图的Excel文件的功能。在有光泽的服务器上运行相同的应用程序,产生错误
无法打开文件'Rplots.pdf'
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)
答案 0 :(得分:1)
我想提供一个最小的例子,因为我一直在为openxlsx
的{{1}}函数无法在Shiny-server内部工作而苦恼,这可能是由于insertPlot
或tempfile
。最适合我的方法是使用dev.copy
,因为它可以非常巧妙地处理分辨率,并且还具有scale参数,该参数可以调整图像元素的大小,使其更适合演示。我还包括了使用ggsave
函数的第二种方法。
下面是一个可重现的示例,部分来自@sebkopf在Save plots made in a shiny app中的答案
png