首页 > 解决方案 > 提取闪亮 DT 中选定单选按钮的值

问题描述

背景:我在模态对话框中有一个闪亮的 DT,其中包含列中的单选按钮,我想根据 DT 中单选按钮的选择进行一些处理

问题:我无法弄清楚如何从 DT 中提取所选单选按钮的值

以下是相同的可重现示例。

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

shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',

    actionBttn(inputId = "btnContinue",label = "Continue",style = "material-flat")

  ),
  server = function(input, output, session) {

    dtWithRadioButton <- reactiveValues(dt = NULL)

    observeEvent(input$btnContinue,{

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

      dt <- data.table(m)
      v <- LETTERS[1:12]

      dt <- cbind(v,dt)

      dtWithRadioButton$dt <- dt # setting reactive value

      showModal(modalDialog(
        size = "l",easyClose = F,fade = T,
        renderDataTable(datatable(dt, selection = "none",escape = FALSE, options = list(dom = 't') , rownames = F)),
       footer =  tagList(
          actionBttn(inputId = "btnCancel",label = "Cancel",style = "float",size="sm",color="warning"),
          actionBttn(inputId = "btnProcess",label = "Process",style = "float",size="sm",color="success")
        )
      ))
    })

  observeEvent(input$btnProcess,{

    dt <- dtWithRadioButton$dt # accessing the reactive value

    # do some processing based on the radio button selection

  })

  observeEvent(input$btnCancel,{
    removeModal(session)
  })




  }
)

单击“继续”按钮时,会向用户显示一个包含闪亮 DT 和单选按钮的弹出窗口。一旦用户使用单选按钮进行选择。我想在点击 btnProcess 时运行一个进程

标签: rshinydt

解决方案


你可以做:

library(shiny)
library(DT)
library(shinyWidgets)

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))) {
  for(j in seq_len(ncol(m))) {
    m[i, j] <- sprintf(
      '<input type="radio" name="%s" value="%s" %s/>',
      month.abb[i], m[i, j], ifelse(j==1, 'checked="checked"', "")
    )
  }
}

shinyApp(

  ui = fluidPage(
    title = 'Radio buttons in a table',

    actionBttn(inputId = "btnContinue", label = "Continue", 
                 style = "material-flat")

  ),

  server = function(input, output, session) {

    dtWithRadioButton <- reactiveValues(dt = m)

    observeEvent(input$btnContinue,{

      showModal(modalDialog(
        size = "l", easyClose = FALSE, fade = TRUE,
        DTOutput("datatable"),
        footer =  tagList(
          actionBttn(inputId = "btnCancel", label = "Cancel", 
                       style = "float", size="sm", color="warning"),
          actionBttn(inputId = "btnProcess", label = "Process", 
                       style = "float", size="sm", color="success")
        )
      ))
    })

    output$datatable <- renderDT(
      datatable(dtWithRadioButton$dt, selection = "none", escape = 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());"),
                rownames = TRUE), 
      server = FALSE
    )

    observeEvent(input$btnProcess,{
      dt <- dtWithRadioButton$dt # accessing the reactive value
      # do some processing based on the radio button selection
      for(month in month.abb){
        print(paste0(month, ": ", input[[month]]))
      }
    })

    observeEvent(input$btnCancel,{
      removeModal(session)
    })

  }
)

然后所选按钮的值在input$Jan第一行,input$Feb第二行等。


推荐阅读