6

I have a question that falls more into a "best practice" type of inquiry. When using shiny package in r, is it better to create all widgets on the server side using renderUI and then pushing those to the ui via uiOutput? Or, when possible, should all widgets be created on the ui side?

For instance, the two apps below do the same thing but in the second one, I create the sliderInput on the server side and then push that to the ui rather than creating it in the ui side. (Note, this code is pulled from the Hello Shiny page on R Studio)

App 1 - "Standard Approach" creating widget in ui

#ui.R

# Define UI for application that plots random distributions 
library(shiny)
ui1 <- shinyUI(fluidPage(

    # Application title
    titlePanel("Hello Shiny!"),

    # Sidebar with a slider input for number of observations
    sidebarLayout(
        sidebarPanel(
            sliderInput("obs", 
                        "Number of observations:", 
                        min = 1, 
                        max = 1000, 
                        value = 500)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
))

#server.R

# Define server logic required to generate and plot a random distribution
library(shiny)
server1 <- (function(input, output) {

    output$distPlot <- renderPlot({

        # generate an rnorm distribution and plot it
        dist <- rnorm(input$obs)
        hist(dist)
    })

})

runApp(shinyApp(ui = ui1, server = server1))

App 2 - Alternative Approach - Creating Widget on server side

#ui.R

# Define UI for application that plots random distributions 
library(shiny)
ui2 <- shinyUI(fluidPage(

    # Application title
    titlePanel("Hello Shiny!"),

    # slider comes from the si object created in server.R
    sidebarLayout(
        sidebarPanel(
            uiOutput("si")
        ),

        # Show a plot of the generated distribution
        mainPanel(
            plotOutput("distPlot")
        )
    )
))

#server.R

# Define server logic required to generate and plot a random distribution
library(shiny)
server2 <- (function(input, output) {

    #create slider with renderUI
    output$si <- renderUI(
        sliderInput("obs", 
                    "Number of observations:", 
                    min = 1, 
                    max = 1000, 
                    value = 500)
    )

    output$distPlot <- renderPlot({

        # generate an rnorm distribution and plot it
        dist <- rnorm(input$obs)
        hist(dist)
    })

})

runApp(shinyApp(ui = ui2, server = server2))

To me, the second approach is more generalizable so it should win. However, I am no expert and I rarely see this approach being used unless there is a specific reason why you need the widgets to be responsive in some way. The ways in which I have required responsiveness include:

  • There is data loaded on the server side which ends up feeding the widget choices so it is better to only load the data one time on the server side and create the widgets there, rather than load it on the server and ui side.
  • We need to turn off/on widgets and/or allow them to react to other user input

Since the second approach I've presented can handle the above two options, it makes sense to me that it should be used in all cases, even when there is no real need to create the widget on the server side. I notice that when using the second approach, there is a delay, sometimes accompanied by warnings/errors prior to the widgets being loaded. That is the only downside I've noticed about the approach.

Is one of these approaches considered a "best practice?"

Thanks.

1
  • My opinion is that it depends. If the widget can be statically generated and belongs on the initial page, I think it should go in ui. I don't see a good reason to put anything in server if it doesn't need to be there, mostly for performance reasons. Any content generated in server has to be sent through subsequent WebSocket/Ajax requests, and that's why there's a delay.
    – greg L
    Dec 5, 2017 at 3:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.