rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
892 stars 300 forks source link

dropdownMenuOutput and sidebarMenu #229

Closed KZARCA closed 7 years ago

KZARCA commented 7 years ago

Hi, There is an issue resolved in the barbara/renderMenu branch, but still occurring in the master branch: when the selected tab is not the first, and the renderMenu is invalidated, the first tab gets selected. Here is an example:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(
    title = NULL,
    dropdownMenuOutput("top")
  ),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Tab1", tabName = "tab1"),
      menuItem("Tab2", tabName = "tab2")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab1", 
              h1("tab1")
              ),
      tabItem(tabName = "tab2",
              h1("tab2"),
              textInput("foo", "Type something", "")
              )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  output$top <- renderMenu({
    tags$li(class = "dropdown",
            if (input$foo == "") {
              "text1"
            } else {
              "text2"
            }
    )
  })
})

shinyApp(ui, server)

If you type something inside the textInput, tab1 is selected.

dianalow commented 7 years ago

I have a similar problem. My dropdownMenu: output$messageMenu <- renderMenu({ dropdownMenu(....)})

contains reactive values, and tabs will jump back to the first tab when values are changed.

isolate stopped this problem so its probably due to reactive?

bborgesr commented 7 years ago

I apologize for not being very responsive lately. This is a bit surprising (especially becausebarbara/renderMenu is behind master right now). However, I'm not currently working on this project, so unfortunately, the best answer I can give right now is to use a workaround to ensure that you stay on the current tab. See my answer to #232 for more details. I'd recommend using this workaround until I can understand the root of this issue (which I'll leave open until I start working on this package again).

bborgesr commented 7 years ago

Psych! Sorry, forget my last comment... I actually ended up spending some time on this now because it was so surprising at first. Luckily, the fix was surprisingly simple (see PR for details). You should all be able to run you original workaround-less code now if you install shinydashboard from master. Let me know if there's anything not working right.

KZARCA commented 7 years ago

Thank you very much, it seems to work like a charm!

shosaco commented 7 years ago

Thank you, I updated the recent version. Just for info, the version number is not updated in the DESCRIPTION file (don't know whether that matters at all). Thanks anyways!

bborgesr commented 6 years ago

It doesn't really matter, but it should always be up to date to differentiate between the current development version (master here in Github) and the current released version (on CRAN). I've just updated it now. Thanks for the catch.

KZARCA commented 6 years ago

Hi, I notice a similar behaviour with the following code when I click on the actionButton. It seems to be due to Shiny.(un)bindAll:

library(shiny)
library(shinydashboard)
library(shinyjs)

jscode <- 'shinyjs.writeABC = function(){
            Shiny.unbindAll();
            $("#foo").val("abc");
            Shiny.bindAll();
          }
      '

ui <- dashboardPage(
  dashboardHeader(
    title = NULL
  ),
  dashboardSidebar(
    sidebarMenuOutput("menuUI"),
    textInput("foo", "Type something", "")
  ),
  dashboardBody(
    useShinyjs(),
    extendShinyjs(text = jscode),
    tabItems(
      tabItem(tabName = "tab1", 
              h1("tab1")
      ),
      tabItem(tabName = "tab2",
              h1("tab2"),
              actionButton("click", "Write ABC")
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  output$menuUI <- renderMenu({
    sidebarMenu(
      menuItem("Tab1", tabName = "tab1"),
      menuItem("Tab2", tabName = "tab2")
    )
  })
  observeEvent(input$click, {
    js$writeABC()
  })
})

shinyApp(ui, server)
nfonsecaalves commented 6 years ago

How can I install shinydashboard package from master?