【发布时间】:2019-05-05 07:54:35
【问题描述】:
我需要一个闪亮的 DT 数据表,并在列中嵌入单选按钮。 This app 显示了水平按钮的解决方案,因此我开始对其进行调整,以适应垂直情况。矩阵很容易修改(见下面的代码),但是由于我缺乏 JavaScript 知识,我被困在回调部分。有任何想法吗?
更新:除非单选按钮是必须的,否则在DT中使用行选择功能更容易,只需设置single="single",这样只能选择一行。
library(shiny)
library(DT)
m = matrix(
as.character(1:12), nrow = 12, ncol = 5, byrow = FALSE,
dimnames = list(month.abb, LETTERS[1:5])
)
for (i in seq_len(ncol(m))) {
#for (i in 1) {
m[,i ] = sprintf(
'<input type="radio" name="%s" value="%s"/>',
LETTERS[i], m[,i]
)
}
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {
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({
input[["A"]]
})
}
)
【问题讨论】:
标签: javascript r shiny dt