R:在数据表中突出显示一行的问题

时间:2018-09-20 16:01:51

标签: r datatable flexdashboard

我最近一直在学习flexdashboard来制作仪表板。我正在尝试将特定行设置为bold,但是如果我将rownames设置为False,它将无法正常工作。这是一个示例:

# This example sets the 3rd row to bold
df <- data.frame(
a = runif(3),
b = runif(3),
c = runif(3))
library(DT)
df %>%
  datatable(rownames = T,
            options = list(pageLength = 3,
                         searching = F,
                         lengthChange = F,
                         info = F,
                         paging = F,
                         ordering = F,
                         columnDefs = list(list(className = 'dt-center', targets = 0:3)))) %>% 
  formatStyle(
  0,
  target = "row",
  fontWeight = styleEqual(3, "bold"))

如果rownames = F,则上面的示例将不起作用。我不希望显示rownames。是什么原因,我应该如何解决?

1 个答案:

答案 0 :(得分:1)

关闭行名将删除您要突出显示该行的索引。因此,我们需要创建自己的:

df$index <- seq(1,3)

然后,我们创建datatable,并在df$index中使用list(visible = FALSE, targets = c(3))隐藏columnDefs

df %>%
  datatable(rownames = F,
            options = list(pageLength = 3,
                         searching = F,
                         lengthChange = F,
                         info = F,
                         paging = F,
                         columnDefs = list(list(className = 'dt-center', targets = 0:2), list(visible=FALSE, targets = c(3))))) %>% 
  formatStyle('index',
  target = "row",
  # this says 'style rows bold where index == 3'
  fontWeight = styleEqual(3, "bold"))