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

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

假设我有一个以下

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

r shiny dt
1个回答
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'

© www.soinside.com 2019 - 2024. All rights reserved.