I am working on a shiny app in which the user filters interactively a data frame using some widgets. One of my checkbox is called "LOT". What this checkbox is intended to do is to colour yellow those rows in which the value of the column x_LOT or Y_LOT is "true".
I have tried to include a conditional inside renderTable, so that if the input of the checkbox is true, the correspondent rows are coloured, but it did not work. I have tried to write the conditional inside reactive function that I have for the rest of the filters, but it did not work either.
My code is as follows:
# MY DATA FRAME
df <- data.frame(Consequence = c(rep("x",4),rep("y",4),rep("z",4)),
CANONICAL = rep(c("YES","NO"),6),
x_LOT = c(rep("False", 3), rep("True", 5), rep("False",2), "True","False"),
y_LOT = c(rep("False", 8), rep("True",2), rep("False",2)),
x3=c(12,43,64,34,93,16,32,74,84,89,45,67))
write.csv(df, "df.csv")
# MY APP
library(shiny)
library(DT) # for data tables
library(dplyr)
library(shinyWidgets)
library(lazyeval)
library(data.table)
ui <- function(request) {
fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Upload your File",multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,text/plain",".csv")),
pickerInput("Consequence", "Consequence:", choices = NULL, options = list(`actions-box` = TRUE),
selected = NULL, multiple = TRUE ),
prettyCheckbox(inputId = "CANONICAL", label = "CANONICAL", value = FALSE,
outline = TRUE, fill = TRUE, bigger = TRUE, status = 'success',width = NULL),
prettyCheckbox(inputId="LOT", label = "LOT", value = FALSE,
outline= TRUE, fill = TRUE, status = 'success', width = NULL)),
mainPanel(
dataTableOutput("contents")
)))}
server <- function(input, output, session) {
df <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath)
})
observeEvent(df(), {
req(df())
updatePickerInput(session, inputId = "Consequence", choices = levels(df()$Consequence), selected = levels(df()$Consequence))
})
filtered_df <- reactive({
df() %>%
filter( Consequence %in% input$Consequence ) %>%
filter(if (input$CANONICAL == TRUE) CANONICAL == "YES" else !is.na(CANONICAL))
})
output$contents <- renderDT(
filtered_df(),
class = "display nowrap compact", # style
filter = "top")
# if(input$LOT == TRUE){
# cols = names(df())[grepl( "LOT", names(filtered_df()))]
# datatable(filtered_df) %>% formatStyle(
# columns = cols,
# target = 'row',
# backgroundColor = styleEqual("TRUE", 'yellow')
# )}
}
shinyApp(ui, server)
So, in this case, I would expect to have the rows 4 to 11 coloured in yellow when the checkbox "LOT" is pressed.
Thanks,
Rachael