如何在Shiny DT数据表中预先选择行 [英] How to pre-select rows in Shiny DT datatables
本文介绍了如何在Shiny DT数据表中预先选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这个闪亮的datatable 。我想预先选择前5行。我尝试过:
- 使用
callback
在datatable中更改行类。但是,这并不反映在输入$ x1_rows_selected
变量中。 - 使用
.click()
在rowCallback
在选项列表或回调
中。加载页面时无效。但是,当我通过控制台/浏览器开发工具运行相同的代码时,它可以工作(更新input $ x1_rows_selected
)。
回调
JS:
输出$ x1 = DT :: renderDataTable({
pre>
datatable(cars,
rows = $(#x1 tbody tr);
$(rows).slice(0,5)。每个(function(){
$(this).click();
});
)
})
解决方案此功能已添加到 DT (> = 0.1.3)。例如:
库(闪亮)
if(packageVersion('DT')<'0.1.3') devtools :: install_github('rstudio / DT')
库(DT)
shinyApp(
ui = fluidPage(
fluidRow(
h1('客户端处理'),
DT :: dataTableOutput('x1'),
h1('服务器端处理'),
DT :: dataTableOutput('x2')
)
)
server = function(input,output,session){
output $ x1 = DT :: renderDataTable(
iris,server = FALSE,
selection = list (mode ='multiple',selected = c(1,3,8,12))
)
输出$ x2 = DT :: renderDataTable(
iris,server = TRUE,
selection = list(mode ='multiple',selected = rownames(iris)[c(1,3,8,12)])
)
}
)
I'm using Shiny (0.12.0) with DT (0.0.65) for row-selections in this Shiny datatable. I want to pre-select the first 5 rows. I have tried:
- Changing the class of row using
callback
JS in datatable. However, that is not reflecting in theinput$x1_rows_selected
variable. Only the background/highlight changes because of CSS. - Using
.click()
in eitherrowCallback
in the options list or incallback
. This does not work either when loading the page. However, it works (updatesinput$x1_rows_selected
) when I run the same code through the console / browser dev tool.
callback
JS:
output$x1 = DT::renderDataTable({
datatable(cars,
rows = $("#x1 tbody tr");
$(rows).slice(0,5).each(function() {
$(this).click();
});
)
})
解决方案
This feature has been added to DT (>= 0.1.3). Examples:
library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
ui = fluidPage(
fluidRow(
h1('Client-side processing'),
DT::dataTableOutput('x1'),
h1('Server-side processing'),
DT::dataTableOutput('x2')
)
),
server = function(input, output, session) {
output$x1 = DT::renderDataTable(
iris, server = FALSE,
selection = list(mode = 'multiple', selected = c(1, 3, 8, 12))
)
output$x2 = DT::renderDataTable(
iris, server = TRUE,
selection = list(mode = 'multiple', selected = rownames(iris)[c(1, 3, 8, 12)])
)
}
)
这篇关于如何在Shiny DT数据表中预先选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文