简体   繁体   English

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

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

I have the following app:我有以下应用程序:

...
              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)

When I save a static dataset (like iris or mtcars ) the file saves the actual data.当我保存静态数据集(如irismtcars )时,文件会保存实际数据。 However, as can be seen in the images, i am wanting to save the contents of the filtered DT.但是,从图片中可以看出,我想保存过滤后的 DT 的内容。

I thought that is what input$tableid_rows_all was for, but I only get random integer/numeric values.我认为这就是input$tableid_rows_all 的用途,但我只得到随机整数/数字值。 I have always had trouble with this nonsense but I would really like to get it to work because it is such a valuable function.我一直对这种废话感到困扰,但我真的很想让它工作,因为它是如此有价值的功能。

Help?帮助?

图片1 保存 结果

Check this:检查这个:

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. You wanna use rows_selectd because rows_all gives you back all rows in your table您想使用rows_selectd因为rows_all会返回表中的所有rows
  2. You need to substitute tableId by the name of your table ( table1 ) for you您需要将tableId替换为您的表名( table1
  3. You are not getting gibberish, but the index/row number of those rows you have selected (in your case, all)您没有胡言乱语,而是您选择的那些行的索引/行号(在您的情况下,全部)
  4. To retrieve all data instead of row number, you need to mtcars[input$table1_rows_selected,]要检索所有数据而不是行号,您需要mtcars[input$table1_rows_selected,]

I hope this does the trick for you.我希望这对你有用。 Best!最好的事物!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM