r - 隐藏闪亮数据表中的列,但保持可搜索
问题描述
Shiny 中的 DT 包生成一个带有搜索栏的表格,该搜索栏搜索表格中的每一列。我有一列不想在表格中显示的元数据,但是如果我使用搜索栏进行搜索,我仍然希望这些行出现。
例如,下面的应用程序包含一个标题为 的列searchCol
。这个专栏只是字母。我想在实际表中隐藏此列,并且我希望能够b
使用 DT 搜索栏搜索 letter ,并显示第二行。
有没有办法隐藏该列但它仍然可以与搜索栏一起使用?
library(shiny)
library(DT)
ui <- fluidPage(
DTOutput('tbl1'),
)
server <- function(input, output, session) {
output$tbl1 <- DT::renderDT(server = TRUE, {
datatable(
cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
escape = FALSE,
rownames = FALSE,
filter = list(position = "top", clear = FALSE, plain = TRUE),
selection = "single",
options = list(
autoWidth = TRUE,
pageLength = 50,
lengthMenu = c(50, 100, 1000),
dom = 'Blfrtip',
buttons = c('copy', 'excel')
)
)
})
}
shinyApp(ui, server)
解决方案
我已将此处的答案调整为您需要在DT::datatable
. 您可以使用columnDefs
来定义不同列的渲染选项,targets
定义您所指的列。请注意,JS 库datatables
从 0 开始计数列。
library(shiny)
library(DT)
ui <- fluidPage(
DTOutput('tbl1'),
)
server <- function(input, output, session) {
output$tbl1 <- DT::renderDT(server = TRUE, {
datatable(
cbind(data.frame(replicate(3,sample(0:1,26,rep=TRUE))), data.frame(searchCol = letters)),
escape = FALSE,
rownames = FALSE,
filter = list(position = "top", clear = FALSE, plain = TRUE),
selection = "single",
options = list(
autoWidth = TRUE,
pageLength = 50,
lengthMenu = c(50, 100, 1000),
dom = 'Blfrtip',
buttons = c('copy', 'excel'),
columnDefs = list(
list(
targets = 3,
searchable = TRUE,
visible = FALSE
)
)
)
)
})
}
shinyApp(ui, server)
推荐阅读
- json - 如何合并 bash shell 中唯一的对象 TargetGroupARNs 和 TargetGroupArn?
- reactjs - 模块 AppRegistry 不是已注册的可调用模块,并且无法找到变量:常量
- r - 使用 R 可视化图中的所有短路径
- angular - 在 Google Chrome 版本 73 中单击内部卡片不起作用
- eclipse - 如何在项目中包含其他项目
- node.js - 如何让`fcm_options.link`在firebase网络通知中运行
- python - 通过 os.path.join 在 Python 脚本中使用 Jenkins 变量/参数
- c++ - Qt:用 qml 对象连接翻译器
- python - 我无法从我的数据框中删除没有值的行,为什么?
- windows - 使用什么系统颜色来突出显示 delphi/windows 中的必填字段