Using the 'formattable' package I am able to color entries of a data table in a Shiny app. However, now that I have added to my Shiny app a data table with dropdown menus, I am not able to get my original code working to color the individual dropdown items in the dropdown menu (coloring the background and not the text would be the nicest). I either receive an error similar to
Warning in Ops.factor(x) : ‘>’ not meaningful for factors
Or just the string 'undefined' instead of a data table.
What I have so far is this:
server.R (only important parts printed):
# Setting up the input for the buttons in the table
shinyInput <- function(FUN, len, id, dropdown_input, ...) {
print(dropdown_input)
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), label = "", choices = dropdown_input[, i]))
}
return(inputs)
}
# Generate the dropdown items
dropdowns <- data.frame(
probabilities = shinyInput(selectInput, length(category_descriptions), id = 'dropdown_', dropdown_input = input_data))
df$data <- cbind(category, dropdowns)
})
prob_formatter <- formatter("span",
style = x ~ style(color = ifelse(x > 0.07, "green", ifelse(x < 0.07, "red", "black"))))
# Load the datatable after a csv file is uploaded
observeEvent(input$dataset, {
output$table <- DT::renderDataTable( {
return(as.datatable(formattable(df$data, list(probabilities = prob_formatter)), escape = FALSE, selection = 'none')
)})
})
ui.R (only important parts printed):
# Main panel for displaying outputs ----
mainPanel(formattableOutput("table"))
Optional background information: The items in the dropdown menus equal probabilities that are returned by a machine learning algorithm to categorize a bag of words. Eventually the user would be able to select the correct category to provide feedback and improve the underlying model. In order to assist the user it would be nice to color based on the underlying probabilities the background of possibilities returned by the model in the dropdown menus. I have already been reading the documentation of 'formattable' and I think 'as.datatable.formattable' might be a way to go, but it seems to be missing from the functions I have in R (reinstalling the package does not make it appear).