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

时间:2019-03-13 23:26:08

标签: r shiny dt

我有以下应用程序:

beta

当我保存静态数据集(例如... 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) iris)时,文件将保存实际数据。但是,从图像中可以看出,我想保存过滤后的DT的内容。

我认为这就是input$tableid_rows_all的用途,但是我只能得到随机的整数/数值。我一直对这种胡说八道感到麻烦,但是我真的很想让它工作,因为它是如此有价值的功能。

帮助?

pic1 saving resulting

1 个答案:

答案 0 :(得分: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,]

我希望这可以帮到您。 最好!