[英]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.当我保存静态数据集(如iris
或mtcars
)时,文件会保存实际数据。 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?帮助?
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)
rows_selectd
because rows_all
gives you back all rows
in your table您想使用rows_selectd
因为rows_all
会返回表中的所有rows
tableId
by the name of your table ( table1
) for you您需要将tableId
替换为您的表名( table1
)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.