2016-11-17 98 views
2

我学习shiny和我创建一个闪亮的应用程序,应该允许用户:闪亮:下载表数据和情节

1-上传数据.csv文件

2-获取统计和情节

3-下载为.csv表数据和图表为.png

输入文件

我创建的输入.csv文件的应用程序中使用来自diamonds data.frame

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) 

现在,用户可以上传数据,并得到表格和情节,但下载按钮不起作用。我尝试了不同的选项和几个问题,但我无法弄清楚如何让下载按钮起作用。

任何建议将不胜感激?

+1

注意到代码中的一些问题。在'summarise_each'中,删除'funs()'中的括号和点,它们不是必需的。此外,如果您使用ggplot作为图形,只需使用'ggsave()'并将它传递给绘图对象,而不是在闪烁的设备调用中搞乱。 [这个问题](https://stackoverflow.com/questions/26764481/downloading-png-from-shiny-r?rq=1)应该有助于将光照传递给'ggsave()'。 –

+1

您还可以使用'ggplotly' – HubertL

回答

3

您的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) 
    } 
) 

的问题与你的情节是,你需要print他们(和设置内容类型)。

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" 
) 
+0

非常感谢您的时间,帮助和完美的答案。 – aelwan