简体   繁体   中英

Adding radiobutton in shiny DT

I have a DT in which I am adding another column containing radiobuttons using below code.

library(shiny)
library(DT)
library(data.table)


ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
        tags$span("Dummy")
      ),
      mainPanel(
        DT::dataTableOutput("TableWithRadio"))
   )
)

server <- function(input, output) {
   output$TableWithRadio <- DT::renderDataTable({
      dt <- data.table(A = rnorm(1:10), B = rnorm(1:10))
      data.table(dt, Select = shinyInputModelingDB(radioButtons, nrow(dt), "radio_"))
   }, server = FALSE, escape = FALSE, rownames = F,
     options = list(paging = FALSE, searching = F, ordering = F,
     preDrawCallback = JS('function() { 
                          Shiny.unbindAll(this.api().table().node()); }'), 
     drawCallback    = JS('function() { 
                          Shiny.bindAll(this.api().table().node()); } ')

     ))

   shinyInputModelingDB = function(FUN, len, id, ...) { 
     inputs = character(len) 
     for (i in seq_len(len)) { 
       inputs[i] = as.character(FUN(paste0(id, i), choices = "", label = "",
                                 selected = F,  ...)) 
     } 
     return(inputs) 
   }
}

# Run the application 
shinyApp(ui = ui, server = server)

The problem is the radio buttons which I have added are not under the same group due to which user would be able to select multiple radiobuttons(present in different rows)

I would like a DT in which user can select only one radio button from the entire table across rows.

Any ideas how can I go around achieving the same?

Maybe this is could be helpful. The original Q/A came from here . I just removed this line m=t(m) .

The radiobuttons are now exclusive, but the callback function is not working for columns but for rows, so the radioButtons are not returning anything for now.

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]
      )
    }

    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))
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM