簡體   English   中英

為什么 input$tableid_all_rows (DT) 在 Shiny 中工作?

[英]Why is input$tableid_all_rows (DT) working in Shiny?

我有以下應用程序:

...
              selectInput("cars", "Pick a Car: ",
                          c("All" = "All Cars",
                            "Ford" = "Ford",
                            "Volvo" = "Volvo",
                            "Ferrari" = "Ferrari",
                            "Fiat" = "Fiat",
                            "Merc" = "Merc"))
      )),

    shinySaveButton("save", "Save file", "Save file as ...", filetype=list(csv="csv")),
    DT::dataTableOutput('table1')
      )
    )

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  mtcars$car <- rownames(mtcars)

  output$table1 <-renderDataTable({
    mtcars %>%
      filter(stringr::str_detect(car, as.character(input$cars)) | input$cars == 'All Cars')
    })

  observe({
    volumes <- c("UserFolder"="~/Documents/R1/DwnLdWord/saves")
    shinyFileSave(input, "save", roots=volumes, session=session)
    fileinfo <- parseSavePath(volumes, input$save)
    data <- input$table1_rows_all
    if (nrow(fileinfo) > 0) {
      write.csv(data, fileinfo$datapath)
    }
  })
}
# Run the application 
shinyApp(ui = ui, server = server)

當我保存靜態數據集(如irismtcars )時,文件會保存實際數據。 但是,從圖片中可以看出,我想保存過濾后的 DT 的內容。

我認為這就是input$tableid_rows_all 的用途,但我只得到隨機整數/數字值。 我一直對這種廢話感到困擾,但我真的很想讓它工作,因為它是如此有價值的功能。

幫助?

圖片1 保存 結果

檢查這個:

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

  mtcars$car <- rownames(mtcars)

  output$table1 <-renderDataTable({
    mtcars %>%
      filter(stringr::str_detect(car, as.character(input$cars)) | input$cars == 'All Cars')
    })

  observe({
    volumes <- c("UserFolder"="~/Documents/R1/DwnLdWord/saves")
    shinyFileSave(input, "save", roots=volumes, session=session)
    fileinfo <- parseSavePath(volumes, input$save)
     data <- mtcars[input$table1_rows_selected,]
    if (nrow(fileinfo) > 0) {
      write.csv(data, fileinfo$datapath)
    }
  })
}
# Run the application 
shinyApp(ui = ui, server = server)
  1. 您想使用rows_selectd因為rows_all會返回表中的所有rows
  2. 您需要將tableId替換為您的表名( table1
  3. 您沒有胡言亂語,而是您選擇的那些行的索引/行號(在您的情況下,全部)
  4. 要檢索所有數據而不是行號,您需要mtcars[input$table1_rows_selected,]

我希望這對你有用。 最好的事物!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM