Reputation: 421
I have a question about the Output Function in a Shiny application. Is it possible to write an output function with a variable as name to use it multiple times?
For example a short extract:
output$MainBody <- renderUI({
fluidPage(
gradientBox(
title = "Test",
)
)
})
Is it possible to use a function like this:
dt_representation <- function(x){
output$x <- renderUI({
fluidPage(
gradientBox(
title = "Test",
)
)
})
}
And call this funcion with:
dt_representation(MainBody)
Is that a possibility, or doesn't that work in Shiny?
Upvotes: 0
Views: 431
Reputation: 1671
I would strongly recommand to use modules as Pork Chop said.
But it can happen sometime I use such a little "hack" :
library(shiny)
ui <- fluidPage(
uiOutput("all_id")
)
server <- function(input, output) {
# Define function
createUI <- function(x, text) {
output[[x]] <<- renderUI({
div(text)
})
}
# Use function
createUI("id1", "Here is my first UI")
createUI("id2", "Here is my second UI")
# Combine all in one
output$all_id <- renderUI({
do.call(fluidRow, lapply(c("id1","id2"), uiOutput))
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1