本文介绍了提取闪亮DT中所选单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background : I have a shiny DT in a modaldialog box conataining radio buttons in columns, I want to do some processing based on the selections of radio button in DT

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

Problem : I am unable to figure out how to extract the value of the selected radiobutton from DT

下面是相同的可复制示例.

Below is the reproducible exmaple for the same.

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的闪亮DT的弹出窗口.一旦用户使用单选按钮进行选择.我想点击btnProcess来运行一个进程

On click of 'Continue' button a pop-up containing a shiny DT with radio button is displayed to user. Once user makes selection using radio button. I want to run a process on click of btnProcess

推荐答案

您可以这样做:

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 中,等等.

Then the value of the selected button is in input$Jan for the first row, input$Feb for the second row, etc.

这篇关于提取闪亮DT中所选单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:31