如果使用客户端处理,则可以使用input object完成此操作input[["tablename_rows_all"]]。(附加_rows_all到数据表输出插槽的名称)
该_rows_all对象将返回数据框的行索引。downloadHandler启动下载时,可以在其中使用它来对数据帧进行子集化。
library(shiny)
library(DT)
shinyApp(
ui =
shinyUI(
fluidPage(
DT::dataTableOutput("dt"),
p("Notice that the 'rows_all' attribute grabs the row indices of the data."),
verbatimTextOutput("filtered_row"),
downloadButton(outputId = "download_filtered",
label = "Download Filtered Data")
)
),
server =
shinyServer(function(input, output, session){
output$dt <-
DT::renderDataTable(
datatable(mtcars,
filter = "top"),
server = FALSE
)
output$filtered_row <-
renderPrint({
input[["dt_rows_all"]]
})
output$download_filtered <-
downloadHandler(
filename = "Filtered Data.csv",
content = function(file){
write.csv(mtcars[input[["dt_rows_all"]], ],
file)
}
)
})
)
Run Code Online (Sandbox Code Playgroud)