【问题标题】:R: Problem with highlighting a row in datatableR:突出显示数据表中的一行的问题
【发布时间】:2018-09-20 16:01:50
【问题描述】:

我最近一直在学习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。是什么原因,我应该如何解决?

【问题讨论】:

    标签: r datatable flexdashboard


    【解决方案1】:

    关闭行名称会删除您引用的索引以突出显示该行。所以我们需要创建自己的:

    df$index <- seq(1,3)
    

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 1970-01-01
      • 2020-03-10
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多