Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shinyjs show/hide not working from module

Tags:

module

shinyjs

I want to be able to switch various parts of my UI on/off using shinyjs show and hide. I need to access parts of the UI outside the module from within a module. Is this possible?

See the attached app code. The show/hide buttons in the main server code work, but those in the module do not.

Thanks for any suggestions.

exampleUI <- function(id) {
            ns <- NS(id)
            tagList(actionButton(ns("hide_id"), "Module - Hide divs"),
                    actionButton(ns("show_id"), "Module - Show divs"),
                    uiOutput(ns("plot_id")))
    }
    
    shinyUI(fluidPage(
            shinyjs::useShinyjs(),
                    shinyjs::hidden(tags$div(id = "hidden", "hidden")),
                    tags$div(id = "shown", "shown"),
                    actionButton("hide_id", "Hide divs"),
                    actionButton("show_id", "Show divs"),
                    exampleUI("eg")))

    example <- function(input, output, session)
    {
        ns <- session$ns
        
        observeEvent(input$hide_id,
        {
            shinyjs::hide("hidden")
            shinyjs::hide("shown")
        })
        observeEvent(input$show_id,
        {
            shinyjs::show("hidden")
            shinyjs::show("shown")
        })
    }
    
    shinyServer(function(input, output) {
    
        callModule(example, "eg")
        observeEvent(input$hide_id,
        {
            shinyjs::hide("hidden")
            shinyjs::hide("shown")
        })
        observeEvent(input$show_id,
        {
            shinyjs::show("hidden")
            shinyjs::show("shown")
        })
    })
like image 917
jxf Avatar asked Nov 07 '22 18:11

jxf


1 Answers

That wasn't possible until release v1.1 (January, 2020) came out. With that release the parameter asis was introduced. I quote:

When asis=TRUE, the ID will not be namespaced when inside a module.

Here is adapted reprex of the askers code that shows how it works:

library(shiny)
library(shinyjs)

exampleUI <- function(id) {
  ns <- NS(id)
  tagList(actionButton(ns("hide_id"), "Module - Hide divs"),
          actionButton(ns("show_id"), "Module - Show divs"),
          uiOutput(ns("plot_id")))
}


example <- function(input, output, session)
{
  ns <- session$ns
  
  observeEvent(input$hide_id, {
                 shinyjs::hide("hidden", asis = TRUE)
                 shinyjs::hide("shown", asis = TRUE)
                 shinyjs::hide("plot_id")
               })
  observeEvent(input$show_id, {
                 shinyjs::show("hidden", asis = TRUE)
                 shinyjs::show("shown", asis = TRUE)
                 shinyjs::show("plot_id")
               })
  output$plot_id <- renderUI({
    "This is the module calling ..."
  })
}

UI <- function() {
  fluidPage(
    shinyjs::useShinyjs(),
    shinyjs::hidden(tags$div(id = "hidden", "hidden")),
    tags$div(id = "shown", "shown"),
    actionButton("hide_id", "Hide divs"),
    actionButton("show_id", "Show divs"),
    exampleUI("eg"))
}

server <- function(input, output, session) {
  
  callModule(example, "eg")
  observeEvent(input$hide_id, {
                 shinyjs::hide("hidden")
                 shinyjs::hide("shown")
               })
  observeEvent(input$show_id, {
                 shinyjs::show("hidden")
                 shinyjs::show("shown")
               })
}

shinyApp(UI, server)
like image 104
Jan Avatar answered Nov 23 '22 03:11

Jan