0

我写了一个闪亮的代码,我可以选择过滤数据。所以我需要点击过滤器按钮并选择我选择的过滤器。但是,如果我多次单击过滤器按钮,则过滤器将重置为默认值。我该如何解决这个问题?

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)

在这里,我使用了闪亮的模块功能。因此,如果在选择过滤器后再次单击操作按钮,则过滤器会休息。我该如何解决这个问题?

提前致谢。

4

0 回答 0