Please consider the code below:
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)
))
})
})
}
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)
})
filterDataServer('filter', dataset)
plotServer('bar', dataset)
}
shinyApp(ui, server)
I am trying to filter the data_ on the basis of 'species' input values (filterDataServer) and return the modified dataset which I need to use in plotServer, so that the specific check box selection reflects in the plot.
By default it will select all the values of Species and if a user deselect any selection from check box the data will get filtered.