在闪亮的应用程序中预检查数据表输出中的单选按钮
pre check radio buttons in datatable output in shiny app
我正在构建一个闪亮的应用程序,我希望我使用的 DT table 具有预先检查的单选按钮,然后可以在必要时由用户更改。例如在这个例子中:https://yihui.shinyapps.io/DT-radio/,我希望所有的月份都预先选择“A”,然后用户可以将“B”更改为“E”或只保留“A”。
我需要在代码中更改什么才能实现这一点?
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
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, ]
)
}
m
output$foo = DT::renderDataTable(
m, escape = FALSE, selection = 'none', server = FALSE,
options = list(dom = 't', paging = 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({
str(sapply(month.abb, function(i) input[[i]]))
})
}
)
您必须在第一列中添加 checked
属性:
for (i in seq_len(nrow(m))) {
m[i, 1] = sprintf(
'<input type="radio" name="%s" value="%s" checked/>',
month.abb[i], m[i, 1]
)
m[i, 2:5] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
month.abb[i], m[i, 2:5]
)
}
我正在构建一个闪亮的应用程序,我希望我使用的 DT table 具有预先检查的单选按钮,然后可以在必要时由用户更改。例如在这个例子中:https://yihui.shinyapps.io/DT-radio/,我希望所有的月份都预先选择“A”,然后用户可以将“B”更改为“E”或只保留“A”。
我需要在代码中更改什么才能实现这一点?
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
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, ]
)
}
m
output$foo = DT::renderDataTable(
m, escape = FALSE, selection = 'none', server = FALSE,
options = list(dom = 't', paging = 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({
str(sapply(month.abb, function(i) input[[i]]))
})
}
)
您必须在第一列中添加 checked
属性:
for (i in seq_len(nrow(m))) {
m[i, 1] = sprintf(
'<input type="radio" name="%s" value="%s" checked/>',
month.abb[i], m[i, 1]
)
m[i, 2:5] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
month.abb[i], m[i, 2:5]
)
}