DT Shiny中单列的渲染下拉列表 [英] render dropdown for single column in DT shiny

查看:67
本文介绍了DT Shiny中单列的渲染下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不精通Javascript,并且想要复制


I'm not proficient in Javascript and would like to replicate a dropdown function as is available in the rhandsontable package but for the DT package.

How could this be achieved in the most efficient way?

Example


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...
         )
      ))
    ))

The goal is to have the i variable act as validator for the allowed input in the DT object.

Any help is much appreciated!

解决方案

I blatantly stole the idea from Yihui's app for including radioButtons in DT.

Code:

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)

Output:

这篇关于DT Shiny中单列的渲染下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆