问题
This is a slight extension of an earlier question.
Note: This solution now works after a typo was identified in the code and corrected. I hope this is a useful pattern that others can use too.
I would like different output types to be displayed via uiOutput
, but in a modular framework.
What I have so far is:
module_ui <- function(id){
ns <- NS(id)
tagList(
selectInput(ns("output_type"),
label = "Select type of output",
selected = "table",
choices = c("table", "barplot", "graph")
),
uiOutput(ns("diff_outputs"))
)
}
module_server <- function(input, output, session){
ns <- session$ns
output$table <- renderTable({head(women)})
output$barplot <- renderPlot({barplot(women$height)})
output$scatterplot <- renderPlot({plot(women$height ~ women$weight)})
output_selected <- reactive({input$output_type})
output$diff_outputs <- renderUI({
if (is.null(output_selected()))
return()
switch(
output_selected(),
"table" = tableOutput(ns("table")),
"barplot" = plotOutput(ns("barplot")),
"graph" = plotOutput(ns("scatterplot"))
)
})
}
ui <- fluidPage(
titlePanel("Dynamically generated user interface components"),
fluidRow(
module_ui("module")
)
)
server <- function(input, output){
callModule(module_server, "module")
}
shinyApp(ui = ui, server = server)
The problem is that uiOutput
is currently blank.
If a solution were found for this kind of problem, it would be very helpful indeed.
I think only very minor modifications are needed, but I am still new to using shiny modules.
回答1:
It works but you have a typo: taglist
should be tagList
.
来源:https://stackoverflow.com/questions/56598005/how-to-use-shinyrenderui-and-shinyuioutput-to-produce-conditional-outputs-with