daattali/shinyjs

hidden() and hide/show do not work as expected

sanjmeh opened this issue · 3 comments

This has been troubling me for some time.
The problem seems intermittent. When the shiny app gets complex it seems to be more often.
Hence not easy to replicate in a reprex.

e.g.
ui.R

hidden(
box(id = "box1", ...)
)

server.R

observeEvent(event1(),{ 
shinyjs::show(id = "box1")
})
...
observeEvent(event2(),{ 
shinyjs::hide(id = "box1")
})

this apparent simple implementation is not reliably working.
The above hides the box at launch but never shows up.
If I replace hidden(..) wrapper by a direct css approach e.g. box(... style = "display:none;") the initial hide does not work but the show and hide inside server works.

Can you please provide a full reprex - ie. a minimal fully functional shiny app that I can copy-paste to run so that I can see the issue you're seeing?

I got the solution Dean. Sorry for the trouble but perhaps someone else getting stuck can benefit from this. I myself forgot the solution I found last year. So this is surely going to trouble others.

The id argument of the shinydashboardPlus::box() function does not work with shinyjs::show (or hide).
We have to wrap it inside a div, e.g. tags$div(id = "xyz", box(..) ) and then use the new id xyz.
That's the solution.

This is usually the case with "complex" elements, that create multiple HTML tags and assign the given ID to an internal element. I take extra steps to ensure this works as expected with inputs, but unfortunately there is no general way of solving this. Any other functions that work on IDs (for example, shiny's builtin removeUI()) will also fail in such cases.

Closing this issue as it's not a bug