Forked from sharlagelfand/shiny_modules_reactlog_not_independent
Last active
July 12, 2019 14:36
-
-
Save gadenbuie/99c1edc8ed0c870ca577ed1a493ce1e4 to your computer and use it in GitHub Desktop.
shiny modules + reactlog + not independent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, open_tab = reactive("iris")) { | |
ns <- session$ns | |
df <- reactive({ | |
req(open_tab() == "iris") | |
cat("\n[iris] reactive") | |
df <- iris | |
}) | |
observe({ | |
req(open_tab() == "iris") | |
cat("\n[iris] observer") | |
values <- as.character(unique(df()[["Species"]])) | |
updateSelectInput(session, "species", | |
choices = values, | |
selected = values[1] | |
) | |
}) | |
output$speciesplot <- renderPlot({ | |
cat("\n[iris] plot") | |
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, open_tab = reactive("mtcars")) { | |
ns <- session$ns | |
df <- reactive({ | |
req(open_tab() == "mtcars") | |
cat("\n[mtcars] reactive") | |
df <- mtcars | |
}) | |
observe({ | |
req(open_tab() == "mtcars") | |
cat("\n[mtcars] observer") | |
values <- unique(df()[["gear"]]) | |
updateSelectInput(session, "gear", | |
choices = values, | |
selected = values[1] | |
) | |
}) | |
output$gearplot <- renderPlot({ | |
cat("\n[mtcars] plot") | |
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", | |
open_tab = reactive(input$open_tab) | |
) | |
callModule( | |
mod_mtcars, | |
"mtcars", | |
open_tab = reactive(input$open_tab) | |
) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment