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)

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.