【问题标题】:R datatable styleColorBar based on different column基于不同列的R数据表styleColorBar
【发布时间】:2021-05-29 01:20:43
【问题描述】:

我想在我的数据表中有一个 styleColorBar,其中条的大小基于不同的列。

iris %>% 
  select(Species, Petal.Length) %>% 
  group_by(Species) %>% 
  mutate(avg_petal = mean(Petal.Length)) %>% 
  datatable() %>% 
    formatStyle(
        'Petal.Length',
        background = styleColorBar(iris$Petal.Length, 'steelblue'),
        backgroundSize = '100% 90%',
        backgroundRepeat = 'no-repeat',
        backgroundPosition = 'center'
    )

如何根据avg_petal 列向Species 列添加彩条?

这可能吗?

【问题讨论】:

    标签: r datatables


    【解决方案1】:

    您可以使用valueColumns 参数,以便datatable 知道数字列用于格式化,然后将格式化data$avg_petal 传递给styleColorBar

    library(DT)
    
    data <- iris %>% 
      select(Species, Petal.Length) %>% 
      group_by(Species) %>% 
      mutate(avg_petal = mean(Petal.Length))
    
    data %>%  datatable() %>% 
      formatStyle(
        'Species', valueColumns = "avg_petal",
        background = styleColorBar(data$avg_petal, 'steelblue'),
        backgroundSize = '100% 90%',
        backgroundRepeat = 'no-repeat',
        backgroundPosition = 'center'
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多