回调可反应,其中包括按行的单选按钮

问题描述 投票:0回答:1

我可以使用DT表创建包含三列的表。我可以回调表以返回无线电输入。我不知道如何回电

reactable
。如果有人知道请分享想法。

library(shiny)
library(DT)
library(reactable)

shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    h5("DT table"),
    DT::dataTableOutput('foo'),
    tableOutput('sel'),
    hr(),
    h5("Reactable"),
    reactableOutput("foo_reactable")

  ),
  server = function(input, output, session) {
 
    m = matrix(
      c("1","0","2"), nrow = length(filter_names), ncol = 3, byrow = TRUE,
      dimnames = list(filter_names,c("Include","Exclude","Ignore")))

    for (i in seq_len(nrow(m))) {
      m[i, ] = sprintf(
        '<input type="radio" name="%s" value="%s" checked="checked"/>',filter_names[i], m[i, ])}

    m_dat <- as.data.frame(m)
    m_dat$var <- filter_names
    
    output$foo = DT::renderDataTable(
      m_dat, escape = FALSE, selection = 'none', server = 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());")
    )
    
    output$sel = renderTable({
      
      out <- cbind(filter_names, sapply(filter_names, function(i) {input[[i]]}))
      out

    })
    
    output$foo_reactable = renderReactable(
      reactable(m_dat,
                columns = list(
                  Include = colDef(html = TRUE),
                  Exclude = colDef(html = TRUE),
                  Ignore = colDef(html = TRUE)
                ))
    ) 
  } 
)
r shiny reactable
1个回答
0
投票

如果您询问如何从可反应表中检索输入,您可以使用reactable.extra。 有关更多自定义输入,请参阅reactable.extra 文档。这是一个演示;

library(shiny)
library(reactable)
library(reactable.extras)
library(stringr)

shinyApp(
 ui = fluidPage(
   reactable.extras::reactable_extras_dependency(),
   reactableOutput("react"),
   hr(),
   textOutput("include_text"),
   textOutput("exclude_text"),
   textOutput("ignore_text")
 ),
 server = function(input, output) {
   output$react <- renderReactable({
   df <- data.frame(
    Name = c('1', '0', '2'),
    Include = c('Include'),
    Exclude = c('Exclude'),
    Ignore = c('Ignore'),
    stringsAsFactors = FALSE,
    row.names = 1:3
   )
   reactable(
    df,
    columns = list(
      Include = colDef(
        header = '',
        cell = button_extra("include", class = "button-extra")
      ),
      Exclude = colDef(
        header = '',
        cell = button_extra("exclude", class = "button-extra")
      ),
      Ignore = colDef(
        header = '',
        cell = button_extra("ignore", class = "button-extra")
      )
     )
    )
   })

   output$include_text <- renderText({
    create_string('include')
   })

   output$exclude_text <- renderText({
    create_string('exclude')
   })

   output$ignore_text <- renderText({
    create_string('ignore')
   })

   create_string <- function(var){
     req(input[[var]])
     values <- input[[var]]      
     paste0(
      str_to_title(var), ": ",
      paste0("{", paste0(names(values), " : ", unlist(values), 
                       collapse = ", "), "}")
     )
    }
   }
  )
© www.soinside.com 2019 - 2024. All rights reserved.