# use non-exported function from teal.slice
include_css_files <- getFromNamespace("include_css_files", "teal.slice")
include_js_files <- getFromNamespace("include_js_files", "teal.slice")
ChoicesFilterState <- getFromNamespace("ChoicesFilterState", "teal.slice")
library(shiny)
filter_state <- ChoicesFilterState$new(
x = c(LETTERS, NA),
slice = teal_slice(varname = "var", dataname = "data")
)
isolate(filter_state$get_call())
filter_state$set_state(
teal_slice(
dataname = "data",
varname = "var",
selected = "A",
keep_na = TRUE
)
)
isolate(filter_state$get_call())
# working filter in an app
library(shinyjs)
data_choices <- c(sample(letters[1:4], 100, replace = TRUE), NA)
attr(data_choices, "label") <- "lowercase letters"
fs <- ChoicesFilterState$new(
x = data_choices,
slice = teal_slice(
dataname = "data", varname = "variable", selected = c("a", "c"), keep_na = TRUE
)
)
ui <- fluidPage(
useShinyjs(),
include_css_files(pattern = "filter-panel"),
include_js_files(pattern = "count-bar-labels"),
column(4, tags$div(
tags$h4("ChoicesFilterState"),
fs$ui("fs")
)),
column(4, tags$div(
tags$h4("Condition (i.e. call)"), # display the condition call generated by this FilterState
textOutput("condition_choices"), tags$br(),
tags$h4("Unformatted state"), # display raw filter state
textOutput("unformatted_choices"), tags$br(),
tags$h4("Formatted state"), # display human readable filter state
textOutput("formatted_choices"), tags$br()
)),
column(4, tags$div(
tags$h4("Programmatic filter control"),
actionButton("button1_choices", "set drop NA", width = "100%"), tags$br(),
actionButton("button2_choices", "set keep NA", width = "100%"), tags$br(),
actionButton("button3_choices", "set selection: a, b", width = "100%"), tags$br(),
actionButton("button4_choices", "deselect all", width = "100%"), tags$br(),
actionButton("button0_choices", "set initial state", width = "100%"), tags$br()
))
)
server <- function(input, output, session) {
fs$server("fs")
output$condition_choices <- renderPrint(fs$get_call())
output$formatted_choices <- renderText(fs$format())
output$unformatted_choices <- renderPrint(fs$get_state())
# modify filter state programmatically
observeEvent(
input$button1_choices,
fs$set_state(
teal_slice(dataname = "data", varname = "variable", keep_na = FALSE)
)
)
observeEvent(
input$button2_choices,
fs$set_state(
teal_slice(dataname = "data", varname = "variable", keep_na = TRUE)
)
)
observeEvent(
input$button3_choices,
fs$set_state(
teal_slice(dataname = "data", varname = "variable", selected = c("a", "b"))
)
)
observeEvent(
input$button4_choices,
fs$set_state(
teal_slice(dataname = "data", varname = "variable", selected = character(0), keep_na = TRUE)
)
)
observeEvent(
input$button0_choices,
fs$set_state(
teal_slice(dataname = "data", varname = "variable", selected = c("a", "c"), keep_na = TRUE)
)
)
}
if (interactive()) {
shinyApp(ui, server)
}
Run the code above in your browser using DataLab