I am working on a shiny app, I am trying to filter a dataframe based in the input of a checkbox, it is, if the check box is clicked the dataframe is filtered, else dataframe remains the same. If the checkbox is selected only those rows with value "YES" must be select in the column 'CANONICAL'
I have read similar questions (like here: Condition Filter in dplyr based on Shiny input) but I haven't been able to solve mine.
My code is as follows:
library(shiny)
library(DT)
library(dplyr)
library(shinyWidgets)
options(shiny.maxRequestSize = 100*1024^2)
ui <- 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 = c( "x", "y","z"),
options = list(`actions-box` = TRUE),
multiple = TRUE ),
prettyCheckbox(inputId= "CANONICAL", label = "CANONICAL", value = FALSE, outline= TRUE, fill = TRUE, bigger = TRUE, status = 'success',width = NULL),
mainPanel(
dataTableOutput("contents")
)
))
server <- function(input, output) {
output$contents <- renderDT({
req(input$file1)
df <- read.delim(input$file1$datapath,
header = TRUE,
sep = '\t')
# Apply filters
df <- df %>%
filter(Consequence == input$Consequence ) %>%
{ if (input$CANONICAL) filter( CANONICAL == 'YES') }
return(df)
shinyApp(ui, server)
The last filter does not work either if the condition is (input$CANONICAL)
, (input$CANONICAL == TRUE)
or (!is.null(input$CANONICAL))
. I have also tried to define the condition in a separate reactive function, but nothing seems to be working.