Datable:将formatStyle应用于数据子集

时间:2017-10-17 17:37:17

标签: r shiny data-visualization

我希望仅在formatStyle上使用datatable作为子集。

例如。 datatable

我想排除最终的"总数"使用formatStyle时的行。我试图使用下面的代码段(不包括最后一行)

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')
})

但它似乎不起作用。

1 个答案:

答案 0 :(得分:0)

使用虚拟数据集

进行检查
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')