根据不同数据集中的值格式化闪亮数据表(DT)的颜色

dis*_*lus 5 r shiny dt

我正在尝试根据上表中的值格式化DT.例如,我想显示某些内容是否已增加,减少或保持不变.我可以用kable做到这一点,但无法进入下一步我想要克隆一个单元格并在另一个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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

图中显示了所需的输出,我还希望根据reference_df中的值为文本着色,如果可能的话还要背景.

在此输入图像描述

Nic*_*icE 9

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

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'"))
Run Code Online (Sandbox Code Playgroud)

columnDefs部分隐藏了最后4列.

您不能formatString基于值,因此如果要添加箭头,可以修改df_data以在将颜色和箭头传递到之前添加颜色和箭头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)
    )
Run Code Online (Sandbox Code Playgroud)

这会循环显示值,df_data并根据值来更改它们reference_df.您需要escape=Fdatatable调用中作为参数来防止HTML转义.

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