Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sharlagelfand/20646a4c02950c2a0a6d351b70d00a96 to your computer and use it in GitHub Desktop.
Save sharlagelfand/20646a4c02950c2a0a6d351b70d00a96 to your computer and use it in GitHub Desktop.
shiny modules + reactlog + not independent
options(shiny.reactlog = TRUE)
library(shiny)
library(reactlog)
mod_iris_ui <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
2,
selectInput(
inputId = ns("species"),
label = "species",
choices = list(
"loading..." = 1
),
selected = 1
)
),
column(
10,
plotOutput(ns("speciesplot"))
)
)
)
}
mod_iris <- function(input, output, session) {
ns <- session$ns
df <- reactive({
req(ns(input$open_tab) == "iris")
df <- iris
})
observe({
req(ns(input$open_tab) == "iris")
values <- as.character(unique(df()[["Species"]]))
updateSelectInput(session, "species",
choices = values,
selected = values[1]
)
})
output$speciesplot <- renderPlot({
hist(iris[iris$Species == input$species, 1])
})
}
mod_mtcars_ui <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
2,
selectInput(
inputId = ns("gear"),
label = "gear",
choices = list(
"loading..." = 1
),
selected = 1
)
),
column(
10,
plotOutput(ns("gearplot"))
)
)
)
}
mod_mtcars <- function(input, output, session) {
ns <- session$ns
df <- reactive({
req(ns(input$open_tab) == "mtcars")
df <- mtcars
})
observe({
req(ns(input$open_tab) == "mtcars")
values <- unique(df()[["gear"]])
updateSelectInput(session, "gear",
choices = values,
selected = values[1]
)
})
output$gearplot <- renderPlot({
hist(mtcars[mtcars$gear == input$gear, 1])
})
}
ui <- tagList(
navbarPage(
title = "App",
id = "open_tab",
tabPanel(
"iris",
mod_iris_ui("iris")
),
tabPanel(
"mtcars",
mod_mtcars_ui("mtcars")
)
)
)
server <- function(input, output, session) {
callModule(
mod_iris,
"iris"
)
callModule(
mod_mtcars,
"mtcars"
)
}
shinyApp(ui = ui, server = server)
@sharlagelfand
Copy link
Author

this example wasn't great in illustrating the non-independence and i'm still not 100% clear in my actual case what was causing the tabs to be non-independent. but using req() and the open_tab helped significantly and ensured that all of the modules run only when their tabs are open. thank you both very much!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment