在 R 中创建数据表,为数据列分配颜色

问题描述 投票:0回答:1

从给定的数据框中,我尝试为数据列分配颜色,但是它失败了,并出现下面给出的错误

rag_df <- data.frame(Vulnerabilities = c("AV", "BV", "EW",
                                         "FD", "FS", "RE",
                                         "Ps", "SA", "FA"),
`High` = c(21.1, 32.2, 2.12, 4.6, 5.43, 6.56, 7.9, 8.11, 9.13), 
`Transmission` = c(1.11, 2.22, 33.3, 4.44, 5.55, 6.66, 7.77, 8.890, 9.19))

crt_rag_tbl <- function (data, col_name) {
  
  high_rates <- sort(data$`High`)
  transmis_chanls <- sort(data$`Transmission`)
  # Generate colors
  color_intervals <- seq(0, 1, length.out = nrow(data$High) + 1)
  
  clrs_rmp <- colorRamp(c("#FD015B", "#FF7300", "#FFF200", "#A5D700"))(c(0, high_rates / transmis_chanls)) 
  # Extract RGB value
  clrs_df <- tibble(r = clrs_rmp[, 1], 
                    g = clrs_rmp[, 2], 
                    b = clrs_rmp[, 3]) %>%
    mutate(mycolor=paste0("rgb(", paste(r,g,b,sep = ","),")"))
  
  clrs <- pull(clrs_df, mycolor)
  
  # Apply colors to the datatable
  create_db <- datatable(data, rownames = TRUE) %>%
    formatStyle("Vulnerabilities", fontWeight = "bold", 
color = 'white', backgroundColor = styleEqual(data$Vulnerabilities, c("#3CD7D9"))) %>%
    formatStyle( "High", backgroundColor = styleInterval(data$`High`, clrs))
  
  return(create_db)
}
# Example usage:
RA_table <- crt_rag_tbl(rag_df, "High")
RA_table

错误

Error in seq.default(0, 1, length.out = nrow(data$High) + :
argument 'length.out' must be of length 1x

我添加了+1来解决长度问题,但这没有任何区别。

seq(0, 1, length.out = nrow(data$High) + 1)

r dplyr datatable tidyverse
1个回答
0
投票
library(DT)
library(dplyr)

rag_df <- data.frame(Vulnerabilities = c("AV", "BV", "EW",
                                         "FD", "FS", "RE",
                                         "Ps", "SA", "FA"),
                     `High` = c(21.1, 32.2, 2.12, 4.6, 5.43, 6.56, 7.9, 8.11, 9.13), 
                     `Transmission` = c(1.11, 2.22, 33.3, 4.44, 5.55, 6.66, 7.77, 8.890, 9.19))

crt_rag_tbl <- function (data, col_name) {
  
  high_rates <- sort(data$`High`)
  transmis_chanls <- sort(data$`Transmission`)
  # Generate colors
  color_intervals <- seq(0, 1, length.out = length(high_rates))
  
  clrs_rmp <- colorRamp(c("#FD015B", "#FF7300", "#FFF200", "#A5D700"))(c(0, high_rates / transmis_chanls)) 
  # Extract RGB value
  clrs_df <- tibble(r = clrs_rmp[, 1], 
                    g = clrs_rmp[, 2], 
                    b = clrs_rmp[, 3]) %>%
    mutate(mycolor=paste0("rgb(", paste(r,g,b,sep = ","),")"))
  
  clrs <- pull(clrs_df, mycolor)
  
  # Apply colors to the datatable
  create_db <- datatable(data, rownames = TRUE) %>%
    formatStyle("Vulnerabilities", fontWeight = "bold", 
                color = 'white', backgroundColor = styleEqual(data$Vulnerabilities, c("#3CD7D9"))) %>%
    formatStyle( "High", backgroundColor = styleInterval(high_rates, clrs))
  
  return(create_db)
}
# Example usage:
RA_table <- crt_rag_tbl(rag_df, "High")
RA_table

创建于 2024-04-11,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.