Can I return ui and server together as one function?

This might sound weird question but hear me out.

Let's take a simple shiny app -

library(shiny)

ui <- fluidPage(
  textOutput("text1"), 
  plotOutput('plot1')
)

server <- function(input, output, session) {
  output$text1 <- renderText("This is text input")
  output$plot1 <- renderPlot(plot(rnorm(10), rnorm(10)))
}

shinyApp(ui, server)

So in this app, we have 1:1 mapping of input and output UI elements. textOutput with renderText and plotOutput with renderPlot.

Now, what I am trying to do is combine the ui and server part of each element in one function.

text_element <- function(input, output, server) {
  textOutput("text1")
  output$text1 <- renderText("This is text input")
}

plot_element <- function(input, output, server) {
  plotOutput('plot1')
  output$plot1 <- renderPlot(plot(rnorm(10), rnorm(10)))
}

I know this doesn't make sense but this is just to give an idea.

My questions are -

  1. Is this possible? Are there any examples/packages that does this?
  2. If 1) is possible, how do I use it in shinyapp to make it work like the original app?
  3. Are there any downsides of this approach?

Thank you for your time and valuable input.

It doesnt make sense to me I'm afraid; But my experience when people start to ask sort of odd questions of this form.., they are often searching for shiny modules.

ui is just HTML, and can't contain R code. You can't mix the two.

ui
#>  <div class="container-fluid">
#>    <div id="text1" class="shiny-text-output"></div>
#>    <div id="plot1" class="shiny-plot-output" style="width:100%;height:400px;"></div>
#>  </div>

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.