【问题标题】:Datable: apply formatStyle to a subset of dataDatatable:将 formatStyle 应用于数据子集
【发布时间】:2017-10-17 17:37:17
【问题描述】:

我希望在 datatable 上仅将 formatStyle 用于子集。

例如。 datatable

我想在使用 formatStyle 时排除最后的“总计”行。我试图使用下面的 sn-p (不包括最后一行)

artistTable7.data <- reactive({
  df <- allData.filtered()
  tot <- my.sum(df$sold)

  salesByYear.All <- summarise(group_by(df, sale_year),
            pb1 = my.sum(result == "sold" & pricebucket == 1),
            pb2 = my.sum(result == "sold" & pricebucket == 2),
            pb3 = my.sum(result == "sold" & pricebucket == 3),
            pb4 = my.sum(result == "sold" & pricebucket == 4),
            pb5 = my.sum(result == "sold" & pricebucket == 5),
            pb6 = my.sum(result == "sold" & pricebucket == 6),
            pb7 = my.sum(result == "sold" & pricebucket == 7),
            pb8 = my.sum(result == "sold" & pricebucket == 8),
            pb9 = my.sum(result == "sold" & pricebucket == 9),
            pb10 = my.sum(result == "sold" & pricebucket == 10),
            pb11 = my.sum(result == "sold" & pricebucket == 11),
            total = my.sum(result == "sold")
            )

  salesByYear.All["Total (By Bucket)" ,] <- colSums(salesByYear.All)
  salesByYear.All[13,1] <-  "Total"

  datatable(salesByYear.All, filter = "none",
            colnames =  c("SALE YEAR","0 - 10K","10K - 50k", "50k - 250k", "250k - 500k", "500k - 1M", "1M - 2.5M","2.5M - 5M", "5M - 10M","10M - 25M", "25M - 50M", "50M +","TOTAL"),
            rownames = FALSE, caption = "LOTS BY PRICE BUCKET",
            options = list(columnDefs=list(list(targets=0:11, class="dt-center")),
                           paging = FALSE, searching = FALSE, autoWidth = FALSE, lengthChange = FALSE, info = FALSE, ordering = FALSE
            ),
            class = 'cell-border hover order-column compact')  %>%
    formatStyle("pb1", background = styleColorBar(salesByYear.All$pb1[-13], 'lightblue')) %>%
    formatStyle("pb2", background = styleColorBar(salesByYear.All$pb2[-13], 'lightblue'))
    # formatStyle(names(subset(salesByYear.All, select=-c(sale_year), sale_year!= "")), background = styleColorBar(range(subset(salesByYear.All, select=-c(sale_year), sale_year!= "")), 'lightblue'))
    # formatStyle(names(salesByYear.All), background = styleColorBar(salesByYear.All$pb3, 'lightblue'), fontWeight = 'bold')
})

但它似乎不起作用。

【问题讨论】:

  • 能否提供您正在尝试的数据和代码

标签: r shiny data-visualization


【解决方案1】:

用一个虚拟数据集检查这个

library(DT)
options(DT.options = list(pageLength = 5))
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

# using styleColorBar
datatable(df) %>% formatStyle(names(df),
                              background = styleColorBar(range(df), 'lightblue'),
                              backgroundSize = '98% 88%',
                              backgroundRepeat = 'no-repeat',
                              backgroundPosition = 'center')

【讨论】:

  • 您好,感谢您的帮助。我刚刚编辑了代码。不会 options() 为环境设置它。我只希望它用于这个特定的数据表。
猜你喜欢
  • 2021-11-17
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2013-11-19
  • 1970-01-01
  • 2014-06-08
  • 2019-01-14
相关资源
最近更新 更多