2016-11-17 18 views
2

私はshinyを学んだし、私は、ユーザーを有効にする必要があり、光沢のあるアプリを作成しています:シャイニー:ダウンロード・テーブル・データおよびプロット

1- .csvファイル

2 - 取得統計などのデータをアップロードしますプロット

3- .png

入力ファイル

として .csvプロットとして表データをダウンロード私は、入力の.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() 'の中の角かっこを削除しますが、それらは必要ありません。さらに、Figureに対してggplotを使用している場合は、 'ggsave()'を使用して、plotオブジェクトを渡してください。 [この質問](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

関連する問題