Tao Xueting
Tao Xueting

Reputation: 15

Add "NA" option in the sliderinput of shiny app

I want to let the user be able to select "NA" or enter empty values in the sliderInput. Maybe add a "NA" button near the slider input? Is there any way to do that? example Thanks

Upvotes: 1

Views: 512

Answers (1)

jpdugo17
jpdugo17

Reputation: 7116

You can add a checkboxInput and pair it with an if condition inside the server. Here's an example using iris dataset.

library(shiny)
library(shinyWidgets)
library(tidyverse)



iris_df <- iris

#populate with some NA's
set.seed(123)
sample(1:nrow(iris), 10) %>%
  walk(~ {
    iris_df[.x, "Sepal.Width"] <<- NA
  })

ui <- fluidPage(
  checkboxInput("na", "Select NA Values", value = FALSE),
  conditionalPanel(
    condition = "input.na == false",
    sliderInput("sepalw", "Sepal Width Range",
      value = c(median(iris$Sepal.Width), max(iris$Sepal.Width)),
      min = min(iris$Sepal.Width),
      max = max(iris$Sepal.Width)
    )
  ),
  dataTableOutput("table")
)

server <- function(input, output, session) {
  df <- reactive({
    if (!input$na) {
      iris_df %>%
        filter(between(Sepal.Width, input$sepalw[[1]], input$sepalw[[2]]))
    } else {
      iris_df %>% filter(is.na(Sepal.Width))
    }
  })

  output$table <- renderDataTable(
    {
      df()
    },
    options = list(pageLength = 10)
  )
}

shinyApp(ui, server)

Upvotes: 1

Related Questions