-
-
Save tbadams45/38f1f56e0f2d7ced3507ef11b0a2fdce to your computer and use it in GitHub Desktop.
library(shiny) | |
library(dplyr) | |
library(ggplot2) | |
innerModUI <- function(id) { | |
ns <- NS(id) | |
fluidPage(fluidRow( | |
uiOutput(ns("inner_slider")), | |
plotOutput(ns("inner_plot")) | |
)) | |
} | |
innerMod <- function(input, output, session) { | |
output$inner_slider <- renderUI({ | |
sliderInput(session$ns("slider2"), label = "inner module slider", min = round(min(mtcars$mpg)), | |
max = round(max(mtcars$mpg)), value = c(min(mtcars$mpg), max(mtcars$mpg), step = 1)) | |
}) | |
output$inner_plot <- renderPlot({ | |
req(input$slider2) | |
data <- filter(mtcars, between(mpg, input$slider2[1], input$slider2[2])) | |
ggplot(data, aes(mpg, wt)) + geom_point() | |
}) | |
} | |
outerModUI <- function(id) { | |
ns <- NS(id) | |
fluidPage(fluidRow( | |
uiOutput(ns("outer_slider")), | |
plotOutput(ns("outer_plot")), | |
innerModUI(ns("inner")) | |
)) | |
} | |
outerMod <- function(input, output, session) { | |
callModule(innerMod, "inner") | |
output$outer_slider <- renderUI({ | |
sliderInput(session$ns("slider1"), label = "outer module slider", min = round(min(mtcars$mpg)), | |
max = round(max(mtcars$mpg)), value = c(min(mtcars$mpg), max(mtcars$mpg), step = 1)) | |
}) | |
output$outer_plot <- renderPlot({ | |
req(input$slider1) | |
data <- filter(mtcars, between(mpg, input$slider1[1], input$slider1[2])) | |
ggplot(data, aes(mpg, wt)) + geom_point() | |
}) | |
} | |
ui <- fluidPage( | |
fluidRow( | |
outerModUI("outer") | |
) | |
) | |
server <- function(input, output, session) { | |
callModule(outerMod, "outer") | |
} | |
shinyApp(ui = ui, server = server) |
yes, thanks!
Thanks :)
Thank you for sharing, this resolved my issue today!
Very clear example that helped me a lot, thanks!
Helpful! Thanks 👍
Thank you for this example, it helped me.
I have two questions, hope you don't mind.
- Why is session$ns() used in renderUI but not in renderPlot?
- Is there a way to list all environments and all their variables in a Shiny app (in real-time), so as to see what is going on?
@boeingguy2, I posted this a few years ago so it's quite likely that it doesn't work anymore. If you figure out how to get this to work, please feel free to post here so others may benefit.
Thanks so much for this example, it helped me figure out how to resolve a complex nested module problem I was having.
I believe the error message generated in the example is due to incorrect bracket placement in the sliderinput lines of code.
This line (appears twice in the example):
value = c(min(mtcars$mpg), max(mtcars$mpg), step = 1))
Should be this:
value = c(min(mtcars$mpg), max(mtcars$mpg)), step = 1)
session$ns("id")
is a life saver, thank you.
Thanks for posting this solution!