So I have spent hours looking at different threads on github and stackoverflow and have yet to find the answer. I have a DT::dataTable that I need to color cells (red, white and green of various gradients) based upon the value in the cell. I tried to use formatStyle, but shiny wouldn't recognize the column names to color anything. Below is code that I have gotten to work ( i commented out the formatStyle where I had it in place)
library(DT)
library(shiny)
#define UI dataset viewer application
ui<- fluidPage(
shinyUI(verticalLayout(
#Application title
headerPanel("R.A.M. Results"),
#Sidebar with controls to select a dataset and specify root numbers.
sidebarPanel(
fluidRow(
textInput(inputId = "root",label = NULL ,placeholder = "Enter Root number here"),
helpText(
tags$em("Note: The root number must include all 6 digits e.g. 000107")
)
),
fluidRow( # made check boxes to allow users to view data in multiple ways.
column(
8, checkboxGroupInput(inputId = "mods",label = "Options", choices = c("All Policy Items","Expand Table"),
selected = "All Policy Items", inline = FALSE)
),
column(
4,offset = 8, actionButton(inputId = "go", label = "Update")
)
)
),
mainPanel(
div(DT::dataTableOutput("RAMResults"),
style = "font-size:70%; width:50%")
)
))
)
server <- function(input,output) {
#the eventReactive code is the delayed reaction.
root1 <- eventReactive(input$go, {input$root})
#check <- eventReactive(input$expand, {input$root})
PolSummary <- reactive({
if ("Expand Table" %in% input$mods) {
policyDetails <- reactive({
Policy[Policy$Root == root1(),c(2:9,12:14,17,19,21:23,25:26,29,32,34,36:42)]
}) # IF we want DS number (i.e. 1,2,3) add in column 18
ItemDetails <- reactive({
Item[Item$Root == root1(), c(2:9,25:26,35,46,54,62:63,72,74,83,98,109,120,126:132)]
})
} else if (!"Expand Table" %in% input$mods) {
policyDetails <- reactive({
Policy[Policy$Root == root1(),c(2:9,12:14,21,32,37:40)]
}) # IF we want DS number (i.e. 1,2,3) add in column 18
ItemDetails <- reactive({
Item[Item$Root == root1(), c(2:9,25:26,35,62,109,127:130)]
})
}
# IF we want DS number (i.e. 1,2,3) add in column 47
#the if else statment allows to look at just the policy summary data or look at everything,
#if we remove this keep the rbind line "THIS ONE" only
#(rbind (policyDetails(),ItemDetails(),deparse.level = 1)
if ("All Policy Items" %in% input$mods) {
rbind(policyDetails(),ItemDetails(),deparse.level = 1) #THIS ONE
} else if (!"All Policy Items" %in% input$mods) {
rbind(policyDetails(),deparse.level = 1)
}
})
output$RAMResults <- DT::renderDataTable({
if("Expand Table" %in% input$mods) {
DT::datatable(PolSummary(), rownames = FALSE, extensions = c('Buttons','FixedHeader'), options = list(
lengthMenu = c(10,25,50), pageLength = 50, class = 'cell-border', fixedHeader = TRUE, dom = 'Bfrtip', buttons = list(list(extend = 'colvis', columns = c(7:25)))
) #%>%formatStyle(PolSummary()$LoyCorr, color = 'red', backgroundColor = 'blue', fontWeight = 'bold' )
)
} else if (!"Expand Table" %in% input$mods) {
DT::datatable(PolSummary(), rownames = FALSE, extensions = c('Buttons','FixedHeader'), options = list(
lengthMenu = c(10,25,50), pageLength = 50, class = 'cell-border', fixedHeader = TRUE, dom = 'Bfrtip', buttons = list(list(extend = 'colvis', columns = c(7:16)))
) #%>%formatStyle(PolSummary()$LoyCorr, color = 'red', backgroundColor = 'blue', fontWeight = 'bold' )
)
}
})
}
#run the ui and server in the same tab and be able to save it with unique identifying names.
shinyApp(ui = ui, server = server)