【问题标题】:Render a colored arrow next to a value using data table in R使用 R 中的数据表在值旁边呈现彩色箭头
【发布时间】:2021-06-26 13:19:31
【问题描述】:

编辑:不知道如何结束我自己的问题,但在这里找到了一种可行的方法: Format color of shiny datatable (DT) according to values in a different dataset

我知道使用formattable 已经提出了这个问题并且answeredformattable 相关,但我想知道使用datatable 是否存在类似的解决方案。

我有一张表,它同时显示负数和正数。

library(tidyverse)
library(DT)
set.seed(5)

data.frame(x = sample(seq(-100, 100), 100, replace = T)) %>% 
  datatable

我想要正数旁边的绿色向上箭头和负数旁边的红色向下箭头,如下所示:

当表作为数据表存在时,谁能告诉我这是否可能?

【问题讨论】:

    标签: r dt


    【解决方案1】:

    我希望答案仍然对您有所帮助.... 是的,这是可能的。

    具体来说,根据值是正数还是负数,我只是将相应的图标粘贴在值的前面。我通过fontawesome库包含图标。

    在调用datatable()中设置参数escape = FALSE很重要,这样html元素就会相应的渲染出来。

    library(tidyverse)
    library(DT)
    library(fontawesome)
    set.seed(5)
    
    x <- sample(seq(-100, 100), 100, replace = T)
    data <- data.frame(x = x)
    data[which(x > 0), 1] <- paste0(fa(name = "arrow-up", fill = "green"), data[which(x > 0), 1])
    data[which(x < 0), 1] <- paste0(fa(name = "arrow-down",fill = "red"), data[which(x < 0), 1])
    
    datatable(data = data, escape = F, options = list(
      columnDefs = list(list(className = 'dt-right', targets = "_all"))
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2019-07-15
      • 2013-05-24
      • 2015-01-18
      相关资源
      最近更新 更多