shinyjs: how to test if a menuItem is currently shown or hidden?

Hello all,

I have a lot of hidden menuitems and some will be shown based on the user navigation and choices.

I am looking for a way to test if a menuItem is currently shown on the sidebar or not.

Any idea?

Thanks,

Maxime

Hi,

Issues with Shiny code can stem from both the reactive Shiny code itself or the regular R code used in the functions. In order for us to help you with your question, please provide us a minimal reprocudible example (Reprex) where you provide a minimal (dummy) dataset and code that can recreate the issue. One we have that, we can go from there. For help on creating a Reprex, see this guide:

Good luck!
PJ

Hi PJ,

I will try to reproduce a small example from a very large one (sorry in advance if too long).

I currently have the following architecture for my code (the only one I could have thought about which does what I want --> BUT I realized it takes far too much time as I add products so I will have to find a more optimal solution absolutely!)

UI BODY SIDE:

dashboardBody(
tabItems(
tabItem(tabName = "tab_selection",
sidebarLayout(

            sidebarPanel(width = 3,
                           radioButtons(...),
                           shiny::conditionalPanel(condition="", radioButtons(...)),
                           shiny::conditionalPanel(condition="",radioButtons(...))),

              mainPanel(
                DT::dataTableOutput("table"),
                actionButton("selection")    
              ))),
    
    tabItem(tabName = "product1",source("project/product1.R")$value),
    tabItem(tabName = "product2",source("project/product2.R")$value),
    tabItem(tabName = "product3",source("project/product3.R")$value
    )))

To sum up, the user arrives to the home page when going on the app. The homepage has a sidebarPanel with some radio buttons which allow him to filter a table that appears on the mainPanel on the right.

The user can choose a product from this table and click on a button that appears below the table.

Each product has a particular productX.ui.R file (box with several tabs with different content) and a particular productX.server.R file.

As you can see from the code above, every productX.ui.R file is executed (which I suppose is one of the reasons why the app is so slow???)

SIDEBAR SIDE:

sidebarMenu(
id = "tabs",
shinyjs::useShinyjs(),
menuItem("Product Selection", tabName = "tab_selection", ...),
shinyjs::hidden(div(id="product1",menuItem("P1 Name",tabName = "product1", ...))),
shinyjs::hidden(div(id="product2",menuItem("P2 Name",tabName = "product2",...))),
shinyjs::hidden(div(id="product3",menuItem("P3 Name",tabName = "product3", ...)))
)

We have many menuItems:

  • one for the homepage
  • one for each product

All menuItems for the products are initially hidden using shinys::hidden()

SERVER SIDE:

server <- function(input, output,session) {
inputdata <- reactive({...})

output$table <- DT::renderDataTable(
inputdata(),
...)

observeEvent(input$selection, {
ID = ...
shinyjs::show(id= ID,anim = TRUE)

  if (ID == "product1") {source("project/product1_server.R",local=TRUE)$value
  }else if(ID == "product2") {source("project/product2_server.R",local=TRUE)$value
  }else if(ID == "product3") {source("project/product3_server.R",local=TRUE)$value
 
  updateTabItems(session, "tabs", ID)
}})

So that when the user chooses a product, the menuItem of this product appears in the sidebar and the user is redirected directly to this menuItem. The particular server file for this product is ran so that I do not have to run all the server files as it will take ages.

I am trying to find a way to optimize this as my App is far too slow (specially at initiation).

Using Profvis is showing me the following:

Do you (or anyone) have a suggestion so that the App does not have the run all the productX_server.R files???

Regards,

Maxime

Hi,

Although you are providing a lot of new information, I can not run the code as it is not a Reprex. Could try and show me what the content of the files your source is and why you are separating the files. If possible, could you give screenshots of the layout and the procedure?

The only way we can tackle this is if we have all the info needed, or al least a good idea of the core of the problem.

IF the info is not sensitive, you could consider uploading the app and part of the data to gitHub to share more easily

Good luck!
PJ

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.