I have written a shiny code where I have option to filter data. So I need to click on filter button and select filters of my choice. But if I click the filter button more than once then the filters got reset with the default values. How could I fix this problem?
library(shiny)
library(dplyr)
library(echarts4r)
filterDataUI <- function(id){
actionButton(NS(id, 'btn'), 'Filter')
}
filterDataServer <- function(id, data_){
stopifnot(is.reactive(data_))
moduleServer(id, function(input, output, session){
observeEvent(input$btn, {
showModal(modalDialog(
title = 'Filter Data',
checkboxGroupInput(NS(id, 'species'), 'Species',choices = unique(data_()$Species),selected = unique(data_()$Species), inline = TRUE),
sliderInput(NS(id, 'sepal_length'), 'Sepal Length', min = min(data_()$Sepal.Length), max = max(data_()$Sepal.Length), value = max(data_()$Sepal.Length))
))
})
choices <- reactive({
cs <- unique(data_()$Species)
if(input$btn>0)
cs <- input$species
cs
})
choices2 <- reactive({
cs <- max(data_()$Sepal.Length)
if(input$btn>0)
cs <- input$sepal_length
cs
})
return(list(
x = choices,
y = choices2))
})
}
plotUI <- function(id){
echarts4rOutput(NS(id, 'plot'))
}
plotServer <- function(id, data_){
moduleServer(id, function(input, output, session){
output$plot <- renderEcharts4r({
data_() %>%
e_charts(Species) %>%
e_bar(Sepal.Length)
})
})
}
ui <- fluidPage(
fluidRow(column(2, filterDataUI('filter'))),
fluidRow(column(8, plotUI('bar')))
)
server <- function(input, output, session) {
dataset <- reactive({
d <- iris
d$Species <- as.character(d$Species)
return(d)
})
fds <- filterDataServer('filter', dataset)
plotServer('bar', reactive(dplyr::filter(dataset(),Species %in% fds$x(), Sepal.Length < fds$y())))
}
shinyApp(ui, server)
Here I have used shiny module functionality. So, if one clicks the action button again after choosing the filter, then the filters get rest. How can I fix this problem?
Thanks in advance.