根据不同数据集中的值设置闪亮数据表(DT)的颜色格式 [英] Format color of shiny datatable (DT) according to values in a different dataset

查看:49
本文介绍了根据不同数据集中的值设置闪亮数据表(DT)的颜色格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据上表中的值格式化DT.例如,我想显示是否增加,减少或保持不变.我可以用kable做到这一点,但是无法进行下一步,我想点击一个单元格并显示所有相关数据到另一个DT中的该值.

I am trying to format the DT according to the values from the previous table. For example, I want to display if something has increased, decreased or remained the same. I could do this with kable but could not get the next step where I want to clik a the cell and show all the data related to that value in another DT.

library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
    mainPanel(
      dataTableOutput("iris_head")
  )
)

server <- function(input, output) {

  #df_data <- iris

  df_data <- head(iris[-5])

  # Just a dataset describing if iris has changed over a month
  # If reference data is of the same size as the original data (df_data). 
  # If reference data is negative I want the cell in the df_data to be green; 
  # If zero blue and if positive then green.
  # I can make changes with ranges within the current range, can we get the color encoding from another table?
  # set the seed
  set.seed(42)
  reference_df <-  (sapply(df_data, function(x) jitter(x, amount = 2)) - df_data) %>% 
    round(. , digits = 0) 

  print(reference_df)


  output$iris_head <- renderDataTable(datatable(df_data, selection = "single" )%>%
                                        formatStyle('Sepal.Width',
                                                    color = styleInterval(c(3.4, 3.8), c('green', 'blue', 'red')),
                                                    backgroundColor = styleInterval(3.4, c('gray', 'yellow'))) %>%
                                        formatString('Sepal.Width', suffix = '<font color="red">&uArr; </font>'))


}

shinyApp(ui = ui, server = server)

在这种情况下, reference_df 是:

Sepal.Length Sepal.Width Petal.Length Petal.Width
        2           1            2           0
        2          -1           -1           0
       -1           1            0           2
        1           1            2          -1
        1           0            2           2
        0           1           -2           2

所需的输出显示在该图中,在这里我也想根据reference_df中的值给文本加上颜色(如果可能).

The required output is shown in the Figure where I also want to color the text and if possible the background according to values in reference_df.

推荐答案

对于文本颜色部分,您可以使用 formatStyle 完成,但是您需要 cbind df_data reference_df ,然后将其传递给 datatable ,并根据第5到8列的值更改第1到第4列的样式:

For the text color part, you could do it with formatStyle but you would need to cbind the df_data and reference_df, then pass it to datatable and change the style of columns 1 to 4 based on the value of columns 5 to 8:

datatable(cbind(df_data,reference_df), selection = "single",
                                                options=list(columnDefs = list(list(visible=FALSE, targets=c(5:8)))))%>%
                                        formatStyle(1:4, valueColumns=5:8,
                                                    color = JS("value < 0 ? 'red' : value > 0 ? 'green' : 'blue'"))

columnDefs部分隐藏了最后4列.

The columnDefs part hides the last 4 columns.

您不能基于值 formatString ,因此,如果要添加箭头,可以在将 df_data 传递给之前修改 df_data 以添加颜色和箭头.>数据表:

You can't formatString based on values so if you want to add the arrows, you could modify df_data to add the colors and arrows before passing it to datatable:

  for(col in 1:dim(df_data)[2]){
    df_data[col] <- mapply(function(i,j){
      ifelse(i > 0, paste0("<span style='color:red'>",j,"<font>&uArr; </font></span>"),
             ifelse(i<0, paste0("<span style='color:green'>",j,"<font>&dArr; </font></span>"),
                    paste0("<span style='color:blue'>",j,"<font>&hArr; </font></span>")))
    },reference_df[col],df_data[col])
  }

  output$iris_head <- renderDataTable(
    datatable(df_data, selection = "single",escape = F)
    )

这会循环遍历 df_data 的值,并根据 reference_df 的值进行更改.您需要 escape = F 作为 datatable 调用中的参数,以防止HTML转义.

This loops through the values of df_data and changes them depending on the values of reference_df. You need escape=F as an argument in the datatable call to prevent HTML escaping.

如果要为背景等着色,可以在 span 标记中添加更多CSS样式.

You can add more CSS styling in the span tags if you want to color the background etc.

这篇关于根据不同数据集中的值设置闪亮数据表(DT)的颜色格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆