简体   繁体   中英

Adding radio button to the DT Data Table in R Shiny

I need to add radio button to one of the column of DT Data Table and on selection of radio button need to have the popup with a button. I am able accomplish the same using action button , looking for the ways to achieve the same using radio button. Code with action button:

library(shiny)
library(DT)
library(shinyBS)

shinyApp(
ui <- fluidPage(
actionButton("Refresh","Refresh"),
br(),
br(),
DT::dataTableOutput("table"),uiOutput("popup")
),

server <- function(input, output,session) {

shinyInput <- function(FUN, len, id, ...) {
  inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), ...))
  }
  inputs
}

df <- reactiveValues(data = data.frame(
  cbind(Delete = shinyInput(actionButton,nrow(mtcars),'button_', label = " ",onclick = 'Shiny.onInputChange(\"select_button\",  this.id)'),
        mtcars)
))

output$table <- DT::renderDataTable(
  df$data, server = FALSE, escape = FALSE, selection = 'none'
)

observeEvent(input$select_button, {
  toggleModal(session, "modalExample", "open")
})

SelectedRow <- eventReactive(input$select_button,{
  as.numeric(strsplit(input$select_button, "_")[[1]][2])
})

output$popup <- renderUI({
  bsModal("modalExample", "Do you want to delete the row?", "", size = "large",
          actionButton("Delete","Delete")
  )
})

observeEvent(input$Refresh,{
  mtcars <<- retrieveValues()
  df$data <-  data.frame(
    cbind(Delete = shinyInput(actionButton,nrow(mtcars),'button_', label = HTML('<input type="radio" name="radio" value="1"/>'),onclick = 'Shiny.onInputChange(\"select_button\",  this.id)'),
          mtcars)
  )
})

}
)

Code

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(

    '<input type="radio" name="%s" value="%s"/>',

    "C", month.abb[i]
  )
}
m
output$foo = DT::renderDataTable(
  m, escape = FALSE, selection = 'single', server = FALSE,
  options = list(dom = 't', paging = FALSE, ordering = FALSE)
)
output$test <- renderPrint(str(input$C))

output$popup <- renderUI({
  bsModal("modalExample", "Do you want to delete the row?", "", size = "large",
          actionButton("Delete","Delete")
  )
})

observeEvent(input$C, {

  #print("TESTING")

  showModal(modalDialog(
    title = "Do you want to delete the row?",
    actionButton("delete","Delete"),
    size = "l",
    easyClose = TRUE,
    fade = TRUE,
    footer = tagList(
      modalButton("Close")
    )

  ))

})

})

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