【问题标题】:render dropdown for single column in DT shiny为 DT 闪亮中的单列呈现下拉列表
【发布时间】:2019-12-04 12:53:24
【问题描述】:

我不精通 Javascript,想复制rhandsontable 包中提供的下拉功能,但DT 包中提供。

如何以最有效的方式实现这一目标?

例子


library(DT)

i <- 1:5

datatable(iris[1:20, ],
          editable = T,
          options = list(
                    columnDefs = list(
                                      list(
                                           targets = 5,
                                           render = JS(
                     # can't get my head around what should be in the renderer...
         )
      ))
    ))

目标是让i 变量充当DT 对象中允许输入的验证器。

非常感谢任何帮助!

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    我公然从Yihui's app 那里窃取了这个想法,因为我将radioButtons 包含在DT 中。

    代码:

    library(shiny)
    library(DT)
    
    ui <- fluidPage(
      title = 'Selectinput column in a table',
      h3("Source:", tags$a("Yihui Xie", href = "https://yihui.shinyapps.io/DT-radio/")),
      DT::dataTableOutput('foo'),
      verbatimTextOutput('sel')
    )
    
    server <- function(input, output, session) {
      data <- head(iris, 5)
    
      for (i in 1:nrow(data)) {
        data$species_selector[i] <- as.character(selectInput(paste0("sel", i), "", choices = unique(iris$Species), width = "100px"))
      }
    
      output$foo = DT::renderDataTable(
        data, 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-container');
          });
          Shiny.unbindAll(table.table().node());
          Shiny.bindAll(table.table().node());")
      )
      output$sel = renderPrint({
        str(sapply(1:nrow(data), function(i) input[[paste0("sel", i)]]))
      })
    }
    
    shinyApp(ui, server)
    

    输出:

    【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2020-11-14
    • 2016-01-11
    • 2016-01-02
    • 2021-08-28
    • 2019-08-03
    • 2014-04-05
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多