2017-06-17 6 views
0

私がフィルタリングした行のみをダウンロードできるようにするには、ずっと時間を費やしてきました。私は期待通りにrows_selectedを取得しましたが、rows_allは選択された行の数を返していますが、 'G'の行の代わりに正しい行が返されます。& 'H'はダウンロードが 'A' & 'B'を返します。R Shiny rows_allフィルタリングされた行を返すために予期しない結果が返ってくる

私はこの機能をアプリケーションに適用する前に、この機能を理解するための簡単なアプリを作成しました。以下はコードです。

ご協力いただきありがとうございます。私はこれをreddit.com/r/rlanguageに投稿しましたが、はるかに多くの視聴者のためにここに投稿しています。ダウンロードするとき、あなたはあなたのフィルタテーブルを参照するが、元のテーブル、そしてあなたは、元のテーブル上で濾過行番号を適用していないので、

library(shiny) 
library(DT) 
library(dplyr) 
library(scales) 

DS <- data.frame(PRODUCT = c("A","B","C","D","E","F","G","H","I","J"), 
      UNITS = runif(n = 10, min = 0, max = 100), 
      REVENUE = runif(n = 10, min = 1000, max = 100000)) 

DS <- DS %>% mutate(PRICE = REVENUE/UNITS) 

# Define UI for application 
ui <- fluidPage(
titlePanel("Download Selected Data Example"), 
    br(), 
    fluidRow(
    column(4, 
      selectInput("product", 
         "Select one of the following products:", 
         c("ALL", 
         unique(as.character(DS$PRODUCT))), 
         multiple = T))), 
    br(), 
    fluidRow(
    DT::dataTableOutput("ds"), 
    downloadButton("downloadFiltered", "Download Filtered Rows"), 
    downloadButton("downloadSelected", "Download Selected Rows"))) 

# Define server logic 
server <- function(input, output) { 
output$ds <- DT::renderDataTable({ 

data <- DS 

if (input$product != "ALL"){ 
    data <- data[data$PRODUCT %in% input$product,] 
} 

data 

}, 
rownames = T, 
server = F) 

# download filtered rows 
output$downloadFiltered <- downloadHandler(
filename = "filteredData.csv", 
content = function(file){ 
    s = input$ds_rows_all 
    write.csv(DS[s, , drop = F], file, row.names = T) 
}) 

# download selected rows 
output$downloadSelected <- downloadHandler(
    filename = "selectedData.csv", 
    content = function(file){ 
    s = input$ds_rows_selected 
    write.csv(DS[s, , drop = F], file, row.names = T) 
    } 
) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

答えて

2

問題が発生します。あなたが反応値にし、データテーブル、ダウンロードを構築する際にその反応を使用することを保存する場合は、ダウンロードすると、あなたのフィルターテーブルを参照する必要があり、それが可能である:

server <- function(input, output, session) { 

    # store the currently filtered DS in a reactive 
    filteredDS <- reactive({ 
    if (!"ALL" %in% input$product){ 
     return(DS[DS$PRODUCT %in% input$product,]) 
    }else{ 
     return(DS) 
    } 
    }) 

    # display the currently filtered DS 
    output$ds <- DT::renderDataTable({ 
    filteredDS() 
    }, 
    rownames = T, 
    server = F) 

    # download filtered rows 
    output$downloadFiltered <- downloadHandler(
    filename = "filteredData.csv", 
    content = function(file){ 
     s = input$ds_rows_all 
     write.csv(filteredDS()[s, , drop = F], file, row.names = T) 
    }) 

    # download selected rows 
    output$downloadSelected <- downloadHandler(
    filename = "selectedData.csv", 
    content = function(file){ 
     s = input$ds_rows_selected 
     write.csv(filteredDS()[s, , drop = F], file, row.names = T) 
    } 
) 
} 
+0

ありがとうございました!!!私はちょうどあなたのコメントを見たときに私が答えに更新するためにここに来ていたのと同じような応答をredditで得ました。私は本当にあなたの助けに感謝、これはまさに修正プログラムでした! – user2299914

関連する問題