I've got a shiny App where I want to fill numeric column spaces of a matrix output with different colors from low to high according each row value (all of them but 'topic'). I saw in this link a way to color spaces with color = styleInterval()
but I can't figure out a way to fit different colors for each column by topic (taking into account that they won't necesarilly be the same number of topics each time, but they won't ever be more than 15 topics for sure & of course numbers of each column will vary). Important to mention that I want the same colors for each one of those other 3 numeric columns, gradient according to respective values. Could somebody please tell me the way?
# --------------------------------------- Global --------------------------------------- #
#1. App
if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
library(shiny)
#3. Easier data handling
if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
library(dplyr)
#8. Data Table shiny outputs
if("DT" %in% rownames(installed.packages()) == FALSE){ install.packages("DT") }
library(DT)
#--------------------------------------- User Interface ---------------------------------------#
ui <- fluidPage(
DT::dataTableOutput("topic_info_table")
)
#--------------------------------------- Server ---------------------------------------#
server <- function(input, output, session) {
# COLOR TABLE BY TOPIC
bytopic <- NULL
output$topic_info_table <- DT::renderDataTable({
bytopic <- structure(c("Chocolate", "Pineapple", "Coconut", "Jam", "Jelly",
"Soup", "Ice-Cream", "Cake", "Pudin", "Candy", "Pizza", "Rum",
"Vodka", "2016", "2016", "2017", "2016", "2016", "2018", "2016",
"2017", "2016", "2016", "2016", "2017", "2017", "2034", "2036",
"2036", "2029", "2035", "2036", "2035", "2033", "2035", "2035",
"2035", "2034", "2037", "14030.57", "13488.00", "12402.98", "16053.32",
"13256.43", "11388.83", "12005.04", "13691.61", "13161.59", "12605.35",
"12348.48", "12872.83", "10963.04"), .Dim = c(13L, 4L), .Dimnames = list(
c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13"), c("topic", "year", "expiration", "cost")))
DT::datatable(bytopic, options = list(pageLength = 15)) %>% formatCurrency(c('cost'))
})
}
shinyApp(ui,server)
Idea would be getting something like:
Easily done with Excel's
conditional formating
where you format cells based on their values. In the example I used the clearest green shade for minimum value to the darkest blue shade for the maximum. Hopefully with a legend that says something like Lowest -> Highest with the color gradient.