我尝试在 DT 表中动态设置
columnDefs
的目标,以根据选择隐藏某些列。
不幸的是,如果我将列更改为隐藏,我将无法再访问表中单选按钮的值。
这里是示例代码(基于DT中的单选按钮):
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
selectInput("colHide","Column to hide",choices = c('F','G','H'),multiple =TRUE),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
# table with radio buttons
m = matrix(
as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE,
dimnames = list(month.abb, LETTERS[1:5])
)
for (i in seq_len(nrow(m))) {
m[i, ] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
month.abb[i], m[i, ]
)
}
m2<-matrix(
LETTERS[6:8], nrow = 12, ncol = 3, byrow = TRUE,
dimnames = list(month.abb, LETTERS[6:8])
)
m<-cbind(m,m2)
# render table
output$foo = DT::renderDataTable(
isolate(m), escape = FALSE,
selection = 'none',
server = FALSE,
options = quoToFunction(list(dom = 'Bfrtip',
paging = TRUE,
pageLength = 6,
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '),
# dynamically hide columns
columnDefs = list(list(targets = input$colHide, visible = FALSE)),
ordering = FALSE)),
callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-radiogroup');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
output$sel = renderPrint({
# print result of radio buttons
sapply(grep("foo_",names(reactiveValuesToList(input)),value = TRUE,invert = TRUE), function(i) input[[i]])
})
}
)
在我的应用程序中,我用
numericInput
尝试了同样的技巧,但它也不起作用。
我想我在JS
部分错过了一些东西。
您需要将 dataTableProxy() 与
showCols()
和 hideCols
结合使用,例如如下图。
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
selectInput("colHide","Column to hide",choices = c('F','G','H'),multiple =TRUE),
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
# table with radio buttons
m = matrix(
as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE,
dimnames = list(month.abb, LETTERS[1:5])
)
for (i in seq_len(nrow(m))) {
m[i, ] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
month.abb[i], m[i, ]
)
}
m2<-matrix(
LETTERS[6:8], nrow = 12, ncol = 3, byrow = TRUE,
dimnames = list(month.abb, LETTERS[6:8])
)
m<-cbind(m,m2)
# render table
output$foo = DT::renderDataTable(
isolate(m), escape = FALSE,
selection = 'none',
server = FALSE,
options = quoToFunction(list(dom = 'Bfrtip',
paging = TRUE,
pageLength = 6,
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '),
# hide all columns
columnDefs = list(list(targets = 1:ncol(m), visible = FALSE)),
ordering = FALSE)),
callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-radiogroup');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
proxy <- dataTableProxy('foo')
# show all columns (currently all are hidden)
showCols(proxy, 1:ncol(m))
observeEvent(input$colHide, {
# show all
showCols(proxy, 1:ncol(m))
# hide the desired ones
hideCols(proxy, match(input$colHide, colnames(m)))
}, ignoreNULL = FALSE)
output$sel = renderPrint({
# print result of radio buttons
sapply(grep("foo_",names(reactiveValuesToList(input)),value = TRUE,invert = TRUE), function(i) input[[i]])
})
}
)