rshinyshinydashboard

R shiny force render of server side input on non active tabItem on loading


I am using R shinyDashboard to create a web app. This app is loaded with landing on some 'overview' tabitem as active. There are also some more tabItems. Assume I have a specific tabItem called 'Settings', where one can control on appearance and filters, which I would like to apply with default values. It uses dynamic values, so it must be located on server side.

The problem is that until I'm not visiting the 'Settings' tabItem, its inputs are not initialized and therefore the 'Overview' tabItem is missing data.

Below is a reproducible very simple example, in which the app should be loaded with the value '10' in the text box (while in practice, it is only populated after visiting 'Settings'):

require(shiny)
require(shinydashboard)

ui<-dashboardPage(skin = "black",
    dashboardHeader(
    ),

    dashboardSidebar(
        sidebarMenu(
            menuItem("Overview", tabName = "overview"),
            menuItem("Settings", tabName = "settings")        )
        ),

        dashboardBody(
            tabItems(
                tabItem("overview",
                uiOutput("textUI")
            ),

            tabItem("settings",
                htmlOutput("FilterUI")
            )
        )
    )
)

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

    output$FilterUI <- renderUI({
        numericInput("selected_filter", "Select value",min=0,max=20,value=10)
    })

    output$textUI <-renderUI({
        box(input$selected_filter)
    })
})

shinyApp(ui, server)

Solution

  • Define a reactiveValues or just a reactive() for the variable that you want to be calculated. Assign it a default (10). Refer to that reactive value for the value argument of the numericInput, and for the output$textUI. observeEvent on the numericInput and update the reactiveValues accordingly.