Adding a radio lens to select a DataTable row in Shiny
I need to add radButtons to select rows in Data, i.e. the selected radio alarm clock must be transmitted to the input. I cannot use inline row selection in DT. I really need to use radio buttons to select a row. This is what you need:
Using https://yihui.shinyapps.io/DT-radio/ I can choose COLUMNS. Es:
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput("test")
),
server = function(input, output, session) {
m = matrix(
c(round(rnorm(24),1), rep(3,12)), nrow = 12, ncol = 3, byrow = F,
dimnames = list(month.abb, LETTERS[1:3])
)
for (i in seq_len(nrow(m))) {
m[i, 3] = sprintf(
if_else(i == 1,
'<input type="radio" name="%s" value="%s" checked="checked"/>',
'<input type="radio" name="%s" value="%s"/>'),
"C", month.abb[i]
)
}
m=t(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() {
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$test <- renderPrint(str(input$C))
}
)
Naively, I tried removing m = t (m) and changing rows to columns in the callback. This doesn't work because the callback function in the example adds a class and id to the latter, which have no counterpart for the column.
Any idea?
source to share
A "dirty" fix could be to wrap an integer datatable in div
with an id C
and shiny-input-radiogroup
:
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
tags$div(id="C",class='shiny-input-radiogroup',DT::dataTableOutput('foo')),
verbatimTextOutput("test")
),
server = function(input, output, session) {
m = matrix(
c(round(rnorm(24),1), rep(3,12)), nrow = 12, ncol = 3, byrow = F,
dimnames = list(month.abb, LETTERS[1:3])
)
for (i in seq_len(nrow(m))) {
m[i, 3] = sprintf(
if_else(i == 1,
'<input type="radio" name="%s" value="%s" checked="checked"/>',
'<input type="radio" name="%s" value="%s"/>'),
"C", month.abb[i]
)
}
m
output$foo = DT::renderDataTable(
m, escape = FALSE, selection = 'none', server = FALSE,
options = list(dom = 't', paging = FALSE, ordering = FALSE)
)
output$test <- renderPrint(str(input$C))
}
)
source to share