【问题标题】:Extracting values of selected radio buttons in shiny DT提取闪亮 DT 中选定单选按钮的值
【发布时间】:2019-02-10 19:08:06
【问题描述】:

背景:我在模态对话框中有一个闪亮的 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 时运行一个进程

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    你可以这样做:

    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,以此类推

    【讨论】:

    • 完美!它有效,我不知道我们可以在模态中使用输出块返回的对象。
    • @RohitSaluja 不客气。如果您满意,请考虑accepting the answer。否则,您的问题将被网站视为未回答。
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2019-08-02
    • 2018-09-07
    • 2021-06-10
    • 2015-09-19
    相关资源
    最近更新 更多