rendering input in UI vs Server

Hi! I was wondering if there's a simple way to benchmark if this app:

library(shiny)

ui <- fluidPage(
  textInput("Input", "Text Input")
)

server <- function(input, output, session) {
}

shinyApp(ui, server)

Is slower to render than this app:

library(shiny)

ui <- fluidPage(
  uiOutput("input_render")
)

server <- function(input, output, session) {
  
  output$input_render <- renderUI({ textInput("input2", "Text Input 2") })
}

shinyApp(ui, server)

Is there no difference? And if there is this may be too simplistic a case to see the difference? If possible, what would be a good example of a use case where one is slower than the other?

First of all, code 1 won't work, because it's not textInput (it's radioButtons)

So code 1's ui should be modified like this to compare.

ui <- fluidPage(
    textInput("Input", "Text Input")
  )

And for comparing performances, I used this code

library(shiny)
fun1 = function(){
  ui <- fluidPage(
    textInput("Input", "Text Input")
  )
  
  server <- function(input, output, session) {
  }
  
  shinyApp(ui, server)
}


fun2 = function(){
  ui <- fluidPage(
    uiOutput("input_render")
  )
  
  server <- function(input, output, session) {
    
    output$input_render <- renderUI({ textInput("input2", "Text Input 2") })
  }
  
  shinyApp(ui, server)
}
microbenchmark::microbenchmark(fun1,fun2, times = 2)

image

And as it's result saids, code 2 will take almost 2 times longer time ( 500 ns vs 1050 ns).

However, I think their performance doesn't show a lot of differences since it's in ns level, even not seconds.


Basically their difference is come from their purpose.
I use renderUI to generate UI element in server. i.e.) dynamically

Here is one example.

  • Assume we build shiny application for visualize some grouped data.

  • Make bar plot as group will not difficult as ggplot exists.

  • however if we want to generate table by group. it's really different problem now, since shiny application doesn't know How many number of table should generated before read data.

  • so to handle this problem, shiny should generate table element dynamically. and I belive that's one of purpose of renderUI.

Regards.

1 Like

Thanks! I realize the use cases are different, I was just curious from a theoretical standpoint. Thank you! (And also sorry about the radio buttons typo, I was playing with which input to use for the reprex :blush: )

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