-1

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).

dkreeft
  • 642
  • 5
  • 17
  • It seems your `x` is seen as a factor .. what is `x` in your code ? – MrSmithGoesToWashington Jan 29 '18 at 14:53
  • If I understand correctly `x` is just a placeholder for whatever column name is called in `formattable`. So in this case `x` is `probabilities` (the column containing the probabilities), which is subsequently evaluated. I have already tried to convert the probabilities to numeric values using https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information but that does not make a difference. I believe `probabilities` does not even contain factors. – dkreeft Jan 29 '18 at 14:56
  • And what happens if you just coment the line about formatter, and do not call formattable in the renderDataTable ? Does it shows the table ? – MrSmithGoesToWashington Jan 29 '18 at 16:05

1 Answers1

0

The way I solved it now was to inject code regarding the colors into the Shiny source code. In that way, you could create conditional formatting.

dkreeft
  • 642
  • 5
  • 17