2016-11-17 93 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