Skip to content

Instantly share code, notes, and snippets.

@SimonCoulombe
Created January 8, 2022 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonCoulombe/da32ac397a5a70ce1be26a54593387a2 to your computer and use it in GitHub Desktop.
Save SimonCoulombe/da32ac397a5a70ce1be26a54593387a2 to your computer and use it in GitHub Desktop.
library(shiny)
library(shinyWidgets)
library(dplyr)
mod_filterDataInputMPG_ui <- function(id){
ns <- NS(id)
shinyWidgets::numericRangeInput(
inputId = ns("mpg_range"),
label = "mpg_range",
value = c(0, 99)
)
}
mod_filterDataInputGo_ui <- function(id){
ns <- NS(id)
actionButton(
inputId = ns("go"),
label = "Go")
}
mod_filterDataInput_server <- function(id,df){
stopifnot(!is.reactive(df)) # df shouldnt be reactive here .. it is mtcars
moduleServer( id, function(input, output, session){
ns <- session$ns
eventReactive(input$go,{
list(
df = reactive(
df %>%
dplyr::filter(
mpg >= input$mpg_range[1] &
mpg <= input$mpg_range[2]
)
),
mpg_min = reactive(input$mpg_range[1]),
mpg_max = reactive(input$mpg_range[2])
)
})
})
}
mod_table1Output_ui <- function(id){
ns <- NS(id)
tagList(
DT::dataTableOutput(ns("table1"))
)
}
mod_table1Output_server <- function(id,df, mpg_min, mpg_max){
stopifnot(is.reactive(df)) # df here should be reactive.. it is mtcars after being filtered by the user-selectable inputs
stopifnot(is.reactive(mpg_min))
stopifnot(is.reactive(mpg_max))
moduleServer( id, function(input, output, session){
ns <- session$ns
output$table1 <- DT::renderDataTable({
df() %>%
select(mpg, cyl, disp) %>%
DT::datatable(
caption = paste0("list of cars with mpg between ", mpg_min(), " and ", mpg_max()),
rownames = FALSE,
escape = FALSE
)
})
})
}
myApp <- function() {
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
mod_filterDataInputMPG_ui("filterDataInput_ui_1"),
mod_filterDataInputGo_ui("filterDataInput_ui_1")
),
mainPanel(
mod_table1Output_ui("table1Output_ui_1")
)
)
)
server <- function(input, output, session) {
data <- mod_filterDataInput_server("filterDataInput_ui_1", mtcars)
mod_table1Output_server("table1Output_ui_1", df= data$df, mpg_min =data$mpg_min, mpg_max = data$mpg_max)
}
shinyApp(ui, server)
}
myApp()
#Listening on http://127.0.0.1:4493
#Warning: Error in $: object of type 'closure' is not subsettable
# 52: is.reactive
# 50: mod_table1Output_server [#2]
# 49: server [#16]
#Error in data$df : object of type 'closure' is not subsettable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment