如何在DataTable / shiny中强制formatStyle()根据其他变量输出格式?

时间:2015-10-27 09:50:46

标签: r datatables shiny

假设我有一个以下datatable对象,它使用mpg列的条件格式:

DT::renderDataTable(
    datatable(mtcars,
      options = list(
        searching = FALSE,
        pageLength = nrow(mtcars),
        dom = 't'
      ),
      rownames = FALSE,
      selection = 'none') %>%
      formatStyle('mpg',
                  background = styleColorBar(mtcars$mpg, 'lightblue'),
                  backgroundSize = '98% 88%',
                  backgroundRepeat = 'no-repeat',
                  backgroundPosition = 'center'))

enter image description here

是否可以为mpg列定义条件格式,但条件格式直方图条的大小是基于其他变量(例如disp)?

1 个答案:

答案 0 :(得分:1)

这应该有效

datatable(mtcars,
          options = list(
            searching = FALSE,
            pageLength = nrow(mtcars),
            dom = 't'
          ),
          rownames = FALSE,
          selection = 'none') %>%
  formatStyle('mpg','disp',
              background = styleColorBar(mtcars$disp, 'lightblue'),
              backgroundSize = '98% 88%',
              backgroundRepeat = 'no-repeat',
              backgroundPosition = 'center')

有关详细信息,请参阅https://rstudio.github.io/DT/010-style.html

'mpg' format depending on 'disp'