r - Shiny:下载表格数据和绘图

标签 r csv download shiny png

我正在学习 shiny 并且正在创建一个 Shiny 的应用程序,它应该使用户能够:

1- 将数据上传为 .csv 文件

2- 获取统计数据和图表

3- 将表格数据下载为 .csv 并将绘图下载为 .png

输入文件

我从 diamonds data.frame 创建了要在应用程序中使用的输入 .csv 文件

library(ggplot2)
df <-  diamonds[1:5000, ]
head(df)
write.csv(df, "df.csv")

App.R

library(ggplot2)
library(dplyr)
library(DT)
library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(fileInput("file","Upload your file"), 
      width =2),
  mainPanel(
   width = 10,
   dataTableOutput("table"),
   downloadButton("downloadtable", "Download the table"),
   tags$br(),
   tags$hr(),
   plotOutput("plot1"),
   downloadButton("downloadplot1", "Download the plot"), 
   tags$br(),
   tags$hr(),
   plotOutput("plot2"),
   downloadButton("downloadplot2", "Download the plot")
   )
)
)

server <- function(input,output){

  data <- reactive({

    file1 <- input$file
    if(is.null(file1)){return()} 

    read.csv(file1$datapath, header=TRUE, sep=',')

  })


  output$table <- renderDataTable({
    if (is.null(data())) { return() }

    df1 <- data()

    df2 <- df1 %>% 
      dplyr::select(cut, color, price) %>% 
      dplyr::group_by(cut, color) %>% 
      dplyr::summarise_each(funs(
        min(.),
        mean(.), 
        median(.),
        max(.),
        sd(.), 
        n() 
      ))  
  })  

  output$downloadtable <- downloadHandler(
    filename = function() {
      paste('stats', '.csv', sep='')
    },
    content = function(file) {
      write.csv(df2, file)
    }
  )

  output$plot1 <- renderPlot({
    if (is.null(data())) { return() }
    df1 <- data()

    ggplot(df1, aes (x =carat, y = price, col = color))+
      geom_point()+
      facet_wrap(~cut)

  }
  )

  output$downloadplot1 <- downloadHandler(
    filename = function() {
      paste('plot1', 'png', sep = ".")
    },
    content = function(file) {
     png(file)

      df1 <- data()

      ggplot(df1, aes (x =carat, y = price, col = color))+
        geom_point()+
        facet_wrap(~cut) 

      dev.off()

    }
  )

  output$plot2 <- renderPlot({
    if (is.null(data())) { return() }
    df1 <- data()

    ggplot(df1, aes (x = price, y = carat, col = color))+
      geom_point()+
      facet_wrap(~clarity)
    }
  )

  output$downloadplot2 <- downloadHandler(
    filename = function() {
      paste('plot2', 'png', sep = ".")
    },
    content = function(file) {
      png(file)

      df1 <- data()

      ggplot(df1, aes (x = price, y = carat, col = color))+
        geom_point()+
        facet_wrap(~clarity)

      dev.off()

    }
  )

}
shinyApp(ui=ui, server = server)

现在,用户可以上传数据并获取表格和图表,但下载按钮不起作用。我尝试了不同的选项和几个问题,但我不知道如何让下载按钮工作。

任何建议都会受到高度赞赏?

最佳答案

您的 CSV 下载问题是 df2 此时不可用。您需要生成它。

output$downloadtable <- downloadHandler(
  filename = function() {
    paste('stats', '.csv', sep='')
  },
  content = function(file) {
    df1 <- data()

    df2 <- df1 %>% 
      dplyr::select(cut, color, price) %>% 
      dplyr::group_by(cut, color) %>% 
      dplyr::summarise_each(funs(
        min(.),
        mean(.), 
        median(.),
        max(.),
        sd(.), 
        n() 
      ))  

    write.csv(df2, file)
  }
)

绘图的问题在于您需要打印它们(并设置内容类型)。

output$downloadplot1 <- downloadHandler(
  filename <- function() {
    paste('plot1', 'png', sep = ".")
  },
  content <- function(file) {
    png(file)

    df1 <- data()

    plot <- ggplot(df1, aes (x =carat, y = price, col = color))+
      geom_point()+
      facet_wrap(~cut) 

    print(plot)

    dev.off()
  },
  contentType = "image/png"
)

关于r - Shiny:下载表格数据和绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666542/

相关文章:

r - 恢复图形参数时的警告

r - 使用受约束的 B 样条近似形状轮廓

ruby-on-rails - Rails CSV 导入导致带有前导 0 的数字困惑

python - 带有 2 行标题并导出到 csv 的 pandas 数据框

mysql - 使用 MySQL 在 CSV 中搜索

pdf - 如何强制自动下载pdf?

r - 用 grid 和 gtable 拆解 ggplot

r - 子集有序组件和相对变量名称

javascript - 包含 JS 文件的替代方法

python - 下载仅在文件名中列出的纬度/经度数据