Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Forked from Vestaxis/app1.R
Last active February 25, 2016 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcheng5/4bdd7ba0f84580f37e91 to your computer and use it in GitHub Desktop.
Save jcheng5/4bdd7ba0f84580f37e91 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"))
)
}
tabUI2 <- function(id) {
ns <- NS(id)
uiOutput(ns("ui"))
}
filtersUI <- function(id, ...) {
ns <- NS(id)
fluidRow(
...
)
}
filters <- function(input, output, session) {
}
tab <- function(input, output, session) {
ns <- session$ns
output$ui <- renderUI({
selectizeInput(ns("select"), "Model", choices = unique(mpg$model))
})
output$filter <- renderText(input$select)
invisible()
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters", tabUI2("tab"))
)
)
server <- function(input, output, session) {
tab <- callModule(tab, "tab")
callModule(filters, "filters")
}
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"))
)
}
tabUI2 <- function(id) {
ns <- NS(id)
uiOutput(ns("ui"))
}
filtersUI <- function(id, ...) {
ns <- NS(id)
fluidRow(
...
)
}
filters <- function(input, output, session, ui) {
}
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))
),
div(
selectizeInput(ns("select"), "Model", choices = model())
)
)
)
})
output$filter <- renderText(input$select)
invisible()
}
ui <- fixedPage(
wellPanel(
tabUI("tab")
),
wellPanel(
filtersUI("filters", tabUI2("tab"))
)
)
server <- function(input, output, session) {
callModule(tab, "tab")
callModule(filters, "filters")
}
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"))
)
}
tabUI2 <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("ui")),
uiOutput(ns("ui2"))
)
}
filtersUI <- function(id, ...) {
ns <- NS(id)
fluidRow(
...
)
}
filters <- function(input, output, session) {
}
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", tabUI2("tab"))
)
)
server <- function(input, output, session) {
callModule(tab, "tab")
callModule(filters, "filters")
}
shinyApp(ui, server)
@ziyadsaeed
Copy link

app2.R doesn't work it just shows two empty wellPanels

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