I have the following Shiny Application:
library(shiny)
list1 <- c(0.2,0.8,0.5)
list2 <- c("element1", "element2", "element3")
df <- data.frame(list1, list2)
UI <- fluidPage(
formattableOutput("table1")
)
Server <- function(input, output) {
output$table1 <- renderFormattable({
formattable(df, list(
list1 = color_tile("green", "red")
))
})
}
shinyApp(ui = UI, server = Server)
Right now by table has some kind of conditional formatting. This works. However, I would like to be able to the boundaries for the formatting myself. So fe - low barrier (green) should be 0 and upper boundary (red) should be 1.
Any suggestions on how I can do that?
I couldn't find any way of manually setting the conditional formatting boundaries using any of formattable's functions. However, it appears that the boundaries are consistently set to the min and max of a given column, so you can solve this by hard coding two rows, containing the desired min and max, and then hiding them using CSS like so:
/*
* www/styles.css
*/
table tr:nth-child(-n + 2) {
display: none;
}
library(shiny)
list1 <- c(0.0,1.0,0.2,0.8,0.5)
list2 <- c("", "", "element1", "element2", "element3")
df <- data.frame(list1, list2)
UI <- fluidPage(
includeCSS("www/styles.css"),
formattableOutput("table1")
)
Server <- function(input, output) {
output$table1 <- renderFormattable({
formattable(df, list(
list1 = color_tile("green", "red")
))
})
}
shinyApp(ui = UI, server = Server)
Just note that this fix will only work if the values you want to display are all between zero and one. If you use anything outside of that range the boundaries will be reset to the new min/max.