Skip to content

Instantly share code, notes, and snippets.

@Vestaxis
Last active February 25, 2016 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vestaxis/8a785b63c7b4566a2954 to your computer and use it in GitHub Desktop.
Save Vestaxis/8a785b63c7b4566a2954 to your computer and use it in GitHub Desktop.
Three Shiny apps that export UI components to a separate Shiny module.
# Simple example of exporting UI to another function (this works)
library(shiny)
tabUI <- function(id) {
ns <- NS(id)
fluidRow(
textOutput(ns("filter"))
)
}
filtersUI <- function(id) {
ns <- NS(id)
fluidRow(
uiOutput(ns("ui"))
)
}
filters <- function(input, output, session, ui) {
observe({
output$ui <- ui()
})
}
tab <- function(input, output, session) {
ns <- session$ns
ui <- renderUI({
selectizeInput(ns("select"), "Model", choices = unique(mpg$model))
})
output$filter <- renderText(input$select)
return(reactive(ui))
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters")
)
)
server <- function(input, output, session) {
tab <- callModule(tab, "tab")
callModule(filters, "filters", tab)
}
shinyApp(ui, server)
# Example of exporting UI where a selectInput is dependent on another input from the same exported UI (this doesn't work)
library(shiny)
tabUI <- function(id) {
ns <- NS(id)
fluidRow(
textOutput(ns("filter"))
)
}
filtersUI <- function(id) {
ns <- NS(id)
fluidRow(
uiOutput(ns("ui"))
)
}
filters <- function(input, output, session, ui) {
observe({
output$ui <- ui()
})
}
tab <- function(input, output, session) {
ns <- session$ns
model <- reactive({
req(!is.null(input$select2))
unique(mpg$model[mpg$class == input$select2])
})
ui <- renderUI({
withTags(
div(
div(
selectizeInput(ns("select2"), "Class", choices = unique(mpg$class))
),
div(
selectizeInput(ns("select"), "Model", choices = model())
)
)
)
})
output$filter <- renderText(input$select)
return(reactive(ui))
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters")
)
)
server <- function(input, output, session) {
tab <- callModule(tab, "tab")
callModule(filters, "filters", tab)
}
shinyApp(ui, server)
# Example of exporting UI where a selectInput is dependent on another input from a different exported UI (this works)
library(shiny)
tabUI <- function(id) {
ns <- NS(id)
fluidRow(
textOutput(ns("filter"))
)
}
filtersUI <- function(id) {
ns <- NS(id)
fluidRow(
uiOutput(ns("ui")),
uiOutput(ns("ui2"))
)
}
filters <- function(input, output, session, ui) {
observe({
output$ui <- ui$ui()
output$ui2 <- ui$ui2()
})
}
tab <- function(input, output, session) {
ns <- session$ns
model <- reactive({
req(!is.null(input$select2))
unique(mpg$model[mpg$class == input$select2])
})
ui <- renderUI({
withTags(
div(
div(
selectizeInput(ns("select2"), "Class", choices = unique(mpg$class))
)
)
)
})
ui2 <- renderUI({
withTags(
div(
div(
selectizeInput(ns("select"), "Model", choices = model())
)
)
)
})
output$filter <- renderText(input$select)
return(list(ui = reactive(ui), ui2 = reactive(ui2)))
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters")
)
)
server <- function(input, output, session) {
tab <- callModule(tab, "tab")
callModule(filters, "filters", tab)
}
shinyApp(ui, server)
library(shiny)
tabUI <- function(id) {
ns <- NS(id)
fluidRow(
textOutput(ns("filter"))
)
}
tabUI2 <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("ui")),
uiOutput(ns("ui2"))
)
}
filtersUI <- function(id) {
ns <- NS(id)
fluidRow(
uiOutput(ns("filters"))
)
}
filters <- function(input, output, session) {
output$filters <- renderUI({
tabUI2("tab")
})
}
tab <- function(input, output, session) {
ns <- session$ns
model <- reactive({
req(!is.null(input$select2))
unique(mpg$model[mpg$class == input$select2])
})
output$ui <- renderUI({
withTags(
div(
div(
selectizeInput(ns("select2"), "Class", choices = unique(mpg$class))
)
)
)
})
output$ui2 <- renderUI({
withTags(
div(
div(
selectizeInput(ns("select"), "Model", choices = model())
)
)
)
})
output$filter <- renderText(input$select)
invisible()
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters")
)
)
server <- function(input, output, session) {
callModule(tab, "tab")
callModule(filters, "filters")
}
shinyApp(ui, server)
@ziyadsaeed
Copy link

the mpg dataset comes from ggplot package, in case anyone else is wondering

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