R 将生成的ggplot散点图保存为PDF文件,而不保存其他文件

R 将生成的ggplot散点图保存为PDF文件,而不保存其他文件,r,ggplot2,shiny,download,R,Ggplot2,Shiny,Download,我很难从ShinyApp中通过ggplot创建的散点图生成PDF文件。我成功地使用了与用户juba to的答案类似的方法,但随后我使用的nearPoints()函数不起作用,并给出了一条错误消息,说明无法找到坐标信息。然后,我使用了谢一辉推荐的ggsave选项,但通过这种策略,我将文件保存在ShinyApp所在的文件夹中。我担心如果我试图在shinyapps.io网站上托管的ShinyApp中使用这个,那么在保存这些临时文件时会出现问题。我还尝试在下载完成后删除该文件,但只要显示绘图,就会创建该

我很难从ShinyApp中通过ggplot创建的散点图生成PDF文件。我成功地使用了与用户juba to的答案类似的方法,但随后我使用的nearPoints()函数不起作用,并给出了一条错误消息,说明无法找到坐标信息。然后,我使用了谢一辉推荐的ggsave选项,但通过这种策略,我将文件保存在ShinyApp所在的文件夹中。我担心如果我试图在shinyapps.io网站上托管的ShinyApp中使用这个,那么在保存这些临时文件时会出现问题。我还尝试在下载完成后删除该文件,但只要显示绘图,就会创建该文件,因此在下载复制的文件后会再次创建该文件。以下是我用来将绘图下载为PDF文件的代码示例(一些重要行):

#### User Interface ----

# Show scatterplot with clicking capability
  plotOutput(outputId = "scatterplot", click = "plot_click")

# Show data table where data points clicked will be shown 
  dataTableOutput(outputId = "datatable")

 # Download button
     downloadButton('dlScatPlot', 'Download plot as PDF')

# Server ----

# Wrap the creation of the scatterplot in a function so the plot can be 
# downloaded as PDF
  makeScatPlot <- function() {
         ## some code to generate a ggplot plot
  }

# Create the scatterplot object the plotOutput function is expecting
  output$scatterplot <- renderPlot({
    # The file saved as ggsave originally will be first saved in the server,      and
    # then in the client side if the Download Button is used
    filename <- paste('scatterPlot_', Sys.Date(), '.pdf', sep='')
    ggsave(filename, makeScatPlot(), width = 11, height = 4, dpi = 300, units = "in")
    makeScatPlot()
  })
# Create data table showing points that have been clicked
  output$datatable <- DT::renderDataTable({
    rows <- nearPoints(df1, input$plot_click) %>%
      select(sample_ID, compound, DOI)
    DT::datatable(rows,  rownames = FALSE)
  })

output$dlScatPlot <- downloadHandler(
    filename = function() {
      paste('scatPlot_', Sys.Date(), '.pdf', sep='')
    },
    content = function(file) {
      file.copy(paste('scatPlot_', Sys.Date(), '.pdf', sep=''), file, overwrite = TRUE)

    # To avoid the accumulation of PDFs in the server
      file.remove(paste('scatPlot_', Sys.Date(), '.pdf', sep=''))
    }
  )
#####用户界面----
#显示具有单击功能的散点图
plotOutput(outputId=“scatterplot”,click=“plot\u click”)
#显示数据表,其中将显示单击的数据点
dataTableOutput(outputId=“datatable”)
#下载按钮
下载按钮('dlScatPlot','downloadplotaspdf')
#服务器----
#将散点图的创建包装在函数中,以便可以创建散点图
#以PDF格式下载

makeScatPlot您可以使用
tempfile(fileext=“.pdf”)
将文件保存为临时文件,而不是将文件保存到特定路径。会话结束后,这些文件将自动删除。因此无需手动删除它们。

您可以使用
tempfile(fileext=“.pdf”)
将它们保存为临时文件,而不是将文件保存到特定路径。会话结束后,这些文件将自动删除。因此,无需手动删除它们。

我最终给出了一个显而易见的答案。我没有在downloadHandler调用中直接调用ggsave,因为我直接使用了。因此,最后,我不在renderPlot()函数中创建文件,而是在downloadHandler中创建文件

 # Create the scatterplot object the plotOutput function is expecting
  output$scatterplot <- renderPlot({
    makeScatPlot()
  })
 # Create the button to download the scatterplot as PDF
 output$dlScatPlot <- downloadHandler(
    filename = function() {
      paste('scatterPlot_', Sys.Date(), '.pdf', sep='')
    },
    content = function(file) {
      ggsave(file, makeScatPlot(), width = 11, height = 4, dpi = 300, units = "in")
    }
    )
#创建plotOutput函数所需的散点图对象

输出$scatterplot我终于给出了一个明显的答案。我没有在downloadHandler调用中直接调用ggsave,因为我直接使用了。因此,最后,我不在renderPlot()函数中创建文件,而是在downloadHandler中创建文件

 # Create the scatterplot object the plotOutput function is expecting
  output$scatterplot <- renderPlot({
    makeScatPlot()
  })
 # Create the button to download the scatterplot as PDF
 output$dlScatPlot <- downloadHandler(
    filename = function() {
      paste('scatterPlot_', Sys.Date(), '.pdf', sep='')
    },
    content = function(file) {
      ggsave(file, makeScatPlot(), width = 11, height = 4, dpi = 300, units = "in")
    }
    )
#创建plotOutput函数所需的散点图对象

输出$scatterplot感谢您使用tempfile的想法。我的问题是我不知道如何使用tempfile()函数。我想它必须与我在为散点图输出使用renderPlot()时使用的ggsave函数共存?为了更好地展示我正在尝试做什么,我编辑了我在问题中发布的示例代码。谢谢您使用tempfile的想法。我的问题是我不知道如何使用tempfile()函数。我想它必须与我在为散点图输出使用renderPlot()时使用的ggsave函数共存?为了更好地展示我正在尝试做什么,我编辑了我在问题中发布的示例代码