r - 在 R Shiny 服务器上保存临时文件

标签 r shiny

我一直在尝试解决开发我的第一个 R Shiny 应用程序时遇到的问题。我的应用程序可以在我的笔记本电脑上运行,但是当我将其上传到 Shiny 服务器上时,我无法让它运行。我希望应用程序在服务器上创建散点图的临时文件,plotOut,然后通过单击“下载散点图”按钮或自动将其导出到 Excel 文件。我在下面发布了我在 reprex 上的最佳尝试。我相信我需要以某种方式使用 tempdir(),但不确定如何使用。提前致谢:D

“ui.R”

library(tidyverse)

library(openxlsx)

 

ui <- fluidPage(

  fluidRow("This is a simplified app to reproduce the difficulties in saving and downloading files on the Shiny server",
           plotOutput("plot", height = 350),
           downloadButton('downloadPlot1', "Download ScatterPlot"),
           downloadButton(
             'dl_excel',
             'Download Results (.xlsx)'
           )
  )
)

“服务器.R”

server <- function(input, output){
  ###create basic plot in ggplot
  plotOut <- reactive(
    ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
  )
 
  ###display plot
  output$plot <- renderPlot({
    plotOut()
  })

  #temp = tempdir() #tried using this in filename to no avail

  fName = paste0('plotOut', Sys.Date(), '.png')

  ##save plot file as png
  output$downloadPlot1 <- downloadHandler(
    filename = function(){fName},
    content = function(file){
      ggsave(file,plot=plotOut())
    })

  ##format excel file for output
  output$dl_excel <- downloadHandler(
    filename = function(){
      paste0('results_', Sys.Date(), '.xlsx')
    },
    content = function(file){
      my_workbook <- createWorkbook()
      addWorksheet(
        wb = my_workbook,
        sheetName = "Sheet1"
      )

      insertImage(my_workbook, sheet = 1,
        file = fName
        , width = 4, height = 4, startRow = 1, startCol = 1, units = "in", dpi = 300
      )
      saveWorkbook(my_workbook, file)
    }
  )
}

最佳答案

另一个论坛的人帮助我解决了这个问题。 将代码更新为以下内容可以解决该问题。

output$dl_excel <-
      downloadHandler(
        filename = function() {
          paste0('results_', Sys.Date(), '.xlsx')
        },

        content = function(file) {
          **tempPNG <- tempfile(fileext = ".png") #create a tempfile to print the plot to
          ggsave(tempPNG, plot = plotOut()) #print the plot to the tempfile**

          my_workbook <-
            createWorkbook()
          addWorksheet(wb = my_workbook,
                       sheetName = "Sheet1")
          insertImage(
            my_workbook,
            sheet = 1,
            **file = tempPNG, #use the tempfile**

            width = 4,
            height = 4,
            startRow = 1,
            startCol = 1,
            units = "in",
            dpi = 300
          )            
          saveWorkbook(my_workbook, file)
        }
      )

关于r - 在 R Shiny 服务器上保存临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69905067/

相关文章:

r - 基于id折叠数据框,将变量的多个值存储到列表中

r - 如何在R语言中的sum()中添加一个函数

html - 是否可以使用固定宽度的 verbatimTextOutput 并在 Shiny 中更改文本行?

r - 具有五种不同颜色的热图颜色键

r - 在 Shiny 的 HTML 中嵌入 R 变量

r - 对 data.frame 中的每一行应用一个函数,并将结果附加到 R 中的 data.frame

r - 无法从另一个容器访问由 docker 容器创建的文件?

r - 在 Shiny 中隐藏 sliderInput 的值

r - 无法获得 flexdashboard modal Shiny react 性来处理 NULL 状态

javascript - Shiny 的加载微调器显示得太频繁