传递 javascript 函数作为参数

Pass javascript function as parameter

有什么方法可以使用 DT::datatable() 将 javascript 函数作为参数传递?我正在尝试使用 colvis button's columnText 参数,但 datatable() 总是转换字符串中的值。

我的table和这个很像(我用的是容器作为head,所以不能用colnames参数)

library(DT)

sketch = htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(rowspan = 2, 'Species'),
      th(colspan = 2, 'Sepal'),
      th(colspan = 2, 'Petal')
    ),
    tr(
      lapply(rep(c('Length', 'Width'), 2), th)
    )
  )
))

# Work
datatable(iris,
          extensions = c('Buttons'),
          options = list(dom = c('Bfrtip'),
                         buttons = list(list(extend = 'colvis'))
          ),
          container = sketch)


# Doesn't work
datatable(iris,
          extensions = c('Buttons'),
          options = list(dom = c('Bfrtip'),
                         buttons = list(list(extend = 'colvis',
                                             columnText = function ( dt, idx, title ) { return (idx+1) }))
          ),
          container = sketch)

你必须使用JS函数:

 columnText = JS("function ( dt, idx, title ) { return (idx+1) }")