为什么 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 的用途,但我只得到随机的 integer/numeric 值。我一直对这些废话感到困扰,但我真的很想让它发挥作用,因为它是一个非常有价值的功能。

帮忙?

检查这个:

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 会返回您 table
  2. 中的所有 rows
  3. 您需要将 tableId 替换为您的 table (table1) 的名字
  4. 您得到的不是乱码,而是您选择的那些行的 index/row 数量(在您的情况下,全部)
  5. 要检索所有数据而不是行号,您需要mtcars[input$table1_rows_selected,]

我希望这对你有用。 最好!