-
-
Save jcheng5/4bdd7ba0f84580f37e91 to your computer and use it in GitHub Desktop.
Three Shiny apps that export UI components to a separate Shiny module.
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
app2.R doesn't work it just shows two empty wellPanels