RinteRface / shinydashboardPlus

extensions for shinydashboard
https://shinydashboardplus.rinterface.com
Other
454 stars 77 forks source link

Tab 2 input elements are not created until they get the focus on #4

Closed lozalojo closed 3 years ago

lozalojo commented 6 years ago

I create an input element called advanced that modifies what's shown in the body. When element is TRUE, I add an extra sliderInput. input.advance is TRUE by default, but when I open the app, the sliderInput is not shown. When I click in the second Tab, then the sliderinput appears.

If I put the input.advance in Tab 1, it works, seems like only Tab 1 elements are created.

if (interactive()) {
  library(shiny)
  library(shinydashboard)
  library(shinydashboardPlus)
  shinyApp(
    ui = dashboardPagePlus(
      header = dashboardHeaderPlus(
        enable_rightsidebar = TRUE,
        rightSidebarIcon = "gears"
      ),
      sidebar = dashboardSidebar(),
      body = dashboardBody(
        uiOutput("uiBody")
      ),
      rightsidebar = rightSidebar(
        background = "dark",
        rightSidebarTabList(
          rightSidebarTabItem(
            id = 1,
            icon = "desktop"
          ),
          rightSidebarTabItem(
            id = 2,
            icon = "desktop"
          )
        ),
        rigthSidebarPanel(
          rightSidebarTabContent(
            id = 1,
            title = "Tab 1",
            numericInput("num", "Number:", 1, min = 0, max = 10)
          ),
          rightSidebarTabContent(
            id = 2,
            title = "Tab 2",
            uiOutput("uiAdvanced")
          )
        )
      ),
      title = "Right Sidebar"
    ),
    server = function(input, output) {
      output$uiBody = renderUI({
        fluidRow(textInput("caption", "Caption", "Data Summary"),
                 numericInput("obs", "Observations:", 10, min = 1, max = 100),
                 conditionalPanel(condition = "input.advanced",
                                  sliderInput(
                                    "obs",
                                    "Number of observations:",
                                    min = 0, max = 1000, value = 500
                                  )
                 ))

      })
      output$uiAdvanced = renderUI({
        checkboxInput("advanced", label = "Show advanced features", value = TRUE)
      })

    }
  )
}
DivadNojnarg commented 6 years ago

Input elements that are in inactive tabs are not calculated and are NULL. Once you click on the second sidebar panel, the problem disappear.
I made a quick fix (notice that your issue would also occur if you are using basic shiny tabPanel functions).

Try reinstall shinydashboardPlus from GitHub:

devtools::install.github("DivadNojnarg/shinydashboardPlus")

If you want to put your checkbox in the second tab, you should tell shinydashboardPlus that the second rightSidebarTabItem is active (TRUE) and that the second rightSidebarTabContent is also active (TRUE). The dashboard will be started with the second tab active and your slider will be rendered immediately.

if (interactive()) {
  library(shiny)
  library(shinydashboard)
  library(shinydashboardPlus)
  shinyApp(
    ui = dashboardPagePlus(
      header = dashboardHeaderPlus(
        enable_rightsidebar = TRUE,
        rightSidebarIcon = "gears"
      ),
      sidebar = dashboardSidebar(),
      body = dashboardBody(
        uiOutput("uiBody")
      ),
      rightsidebar = rightSidebar(
        background = "dark",
        rightSidebarTabList(
          rightSidebarTabItem(
            id = 1,
            icon = "desktop"
          ),
          rightSidebarTabItem(
            id = 2,
            icon = "desktop",
            active = TRUE
          )
        ),
        rigthSidebarPanel(
          rightSidebarTabContent(
            id = 1,
            title = "Tab 1",
            numericInput("num", "Number:", 1, min = 0, max = 10)
          ),
          rightSidebarTabContent(
            id = 2,
            title = "Tab 2",
            active = TRUE,
            uiOutput("uiAdvanced")
          )
        )
      ),
      title = "Right Sidebar"
    ),
    server = function(input, output) {

      output$uiBody <- renderUI({
        fluidRow(
          textInput("caption", "Caption", "Data Summary"),
          numericInput("obs", "Observations:", 10, min = 1, max = 100),
          if (!is.null(input$advanced)){
            if (input$advanced) {
              sliderInput(
                "obs", 
                "Number of observations:", 
                min = 0, 
                max = 1000, 
                value = 500
              )
            }
          }
        )

      })

      output$uiAdvanced <- renderUI({
        checkboxInput(
          "advanced", 
          label = "Show advanced features", 
          value = TRUE
        )
      })

    }
  )
}

This approach will not work if you put two uiOuput items in separate tabItems. For now, you should put all dynamically rendered UI elements in one dedicated tab and set it to active. I will find a better approach soon.

lozalojo commented 6 years ago

Yes, that's a workaround, not a fix.

Also, I miss the option to set the width of the right sidebar.

Thank you.

DivadNojnarg commented 6 years ago

Also, I miss the option to set the width of the right sidebar. See the latest version on GitHub. I added a "width" argument to the rightSidebar

lozalojo commented 6 years ago

Sorry to post here, but lastest development version gives me an error with rightSidebarTabList

Error in rightSidebarTabList(rightSidebarTabItem(id = 1, icon = "desktop"), : could not find function "rightSidebarTabList"

(the same app i posted here).

Going back to cran version fix the problem.

`> sessionInfo() R Under development (unstable) (2018-05-18 r74745) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale: [1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252 LC_MONETARY=Spanish_Spain.1252 [4] LC_NUMERIC=C LC_TIME=Spanish_Spain.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] shinydashboardPlus_0.2.0 shinydashboard_0.7.0 shiny_1.1.0

loaded via a namespace (and not attached): [1] compiler_3.6.0 magrittr_1.5 R6_2.2.2 promises_1.0.1 later_0.7.3 htmltools_0.3.6 [7] tools_3.6.0 yaml_2.1.19 Rcpp_0.12.17 digest_0.6.15 xtable_1.8-2 httpuv_1.4.4.1 [13] mime_0.5

`

DivadNojnarg commented 6 years ago

In the rightSidebar function you do not need rightSidebarTabList, rightSidebarTabItem nor rightSidebarPanel. See the documentation in the official package website or the help(rightSidebar) in Rstudio. I simplified the package since the rightSidebar syntax was too heavy.

lozalojo commented 6 years ago

Ok, it has been changed in the devel version, I will change it

DivadNojnarg commented 6 years ago

V 0.3.0 on CRAN. You do not need to install from GitHub.

DivadNojnarg commented 3 years ago

Fixed in latest devel version