From the given dataframe i am trying to assign colour to the data columns however it is failing with the error given below about the
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
The error
Error in seq.default(0, 1, length.out = nrow(data$High) + :
argument 'length.out' must be of length 1x
I have added +1 to fix the length issue however it does not make any difference.
seq(0, 1, length.out = nrow(data$High) + 1)
color_intervals <- seq(0, 1, length.out = nrow(data$High) + 1)
nrow()
of the vector data$High
returns NULL
. This is because vector objects do not have dimensions like data.frames. I suggest to either do nrow(data)
or as I did length(high_rates)
formatStyle( "High", backgroundColor = styleInterval(data$`High`, clrs))
Throws an error because data$`High`
is not sorted. Thus I replaced it with high_rates
which you previously sorted.
Remove all backticks. They are unnecessary.
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
Created on 2024-04-11 with reprex v2.1.0