Shiny modules and conditionalPanel

shiny
shiny-modules

#1

This is a minimum example of my app without using modules:

library(shiny)

ui <- fluidPage(
  conditionalPanel(condition = 'output.showchat == "Your message"' ,
                   absolutePanel(
                     bottom = 5, right = 20, width = 450, height = 600,
                     draggable = TRUE,
                     actionButton("send", "Send", style='padding:0px; font-size:0%'),
                     absolutePanel(bottom = 120, right = 20, width = 450, height = 500,
                                   wellPanel(id = "chatbox",style = "overflow-y:scroll;
                             max-height: 500px; height: 500px"
                                   )
                     ),
                     absolutePanel(bottom = 10, right = 20, width = 450, height = 100,
                                   wellPanel(id = "textinput",style = "height: 100px",
                                             textInput("message_field",
                                                       textOutput('showchat'),
                                                       width = "450px")
                                   )
                     ),


                     style = "opacity: 0.92"
                   )
  ),

  absolutePanel(bottom = 35, right = 25, width = 15, height = 15,
                actionButton("chatButton", "", icon = icon("comments")))
  )

server <- function(input, output,session) {

  rv.chat <- reactiveValues(show.chat = FALSE)

  observeEvent(input$chatButton, ({
    rv.chat$show.chat <- !(rv.chat$show.chat)
  }))

  output$showchat <- renderText({
    if(rv.chat$show.chat){
      "Your message"
    } else{
      "hidded"
    }
  })

  outputOptions(output, "showchat", suspendWhenHidden = FALSE)


  }

shinyApp(ui, server)

When you click the icon on the bottom right corner, the conditionalPanel appears or disappears.

This is my app using a module:

# App module 

chabotUI <- function(id){
  ns <- NS(id)
  tagList(
    
    conditionalPanel(condition = 'output.showchat == "Your message"' ,
                     absolutePanel(
                       bottom = 5, right = 20, width = 450, height = 600,
                       draggable = TRUE,
                       actionButton(ns("send"), "Send", style='padding:0px; font-size:0%'),
                       absolutePanel(bottom = 120, right = 20, width = 450, height = 500,
                                     wellPanel(id = ns("chatbox"),style = "overflow-y:scroll;
                                             max-height: 500px; height: 500px"
                                     )
                       ),
                       absolutePanel(bottom = 10, right = 20, width = 450, height = 100,
                                     wellPanel(id = ns("textinput"),style = "height: 100px",
                                               textInput(ns("message_field"),
                                                         textOutput(ns('showchat')),
                                                         width = "450px")
                                     )
                       ),
                       
                       
                       style = "opacity: 0.92"
                     )
    ),
    
    absolutePanel(bottom = 35, right = 25, width = 15, height = 15,
                  actionButton(ns("chatButton"), "", icon = icon("comments")))
    
  )
}

chabot <- function(input, output, session){
  
  rv.chat <- reactiveValues(show.chat = FALSE)
  
  observeEvent(input$chatButton, ({
    rv.chat$show.chat <- !(rv.chat$show.chat)
  }))
  
  output$showchat <- renderText({
    if(rv.chat$show.chat){
      "Your message"
    } else{
      "hidded"
    }
  })
  
  outputOptions(output, "showchat", suspendWhenHidden = FALSE)
  
}

# Modularized app
library(shiny)

ui <- fluidPage(
  chabotUI("hello")
)

server <- function(input, output,session) {
  callModule(chabot, "hello")
}

shinyApp(ui, server)

Now, when I click on the icon nothing happens. What am I doing wrong?


#2

I found the problem, i just needed to add the ns option:

# App module

chabotUI <- function(id){
  ns <- NS(id)

  tagList(

    conditionalPanel(condition = 'output.showchat == "Your message"', ns = ns ,
                     absolutePanel(
                       bottom = 5, right = 20, width = 450, height = 600,
                       draggable = TRUE,
                       actionButton(ns("send"), "Send", style='padding:0px; font-size:0%'),
                       absolutePanel(bottom = 120, right = 20, width = 450, height = 500,
                                     wellPanel(id = ns("chatbox"),style = "overflow-y:scroll;
                                             max-height: 500px; height: 500px"
                                     )
                       ),
                       absolutePanel(bottom = 10, right = 20, width = 450, height = 100,
                                     wellPanel(id = ns("textinput"),style = "height: 100px",
                                               textInput(ns("message_field"),
                                                         textOutput(ns('showchat')),
                                                         width = "450px")
                                     )
                       ),


                       style = "opacity: 0.92"
                     )
    ),

    absolutePanel(bottom = 35, right = 25, width = 15, height = 15,
                  actionButton(ns("chatButton"), "", icon = icon("comments")))

  )
}

chabot <- function(input, output, session){

  rv.chat <- reactiveValues(show.chat = FALSE)

  observeEvent(input$chatButton, ({
    rv.chat$show.chat <- !(rv.chat$show.chat)
  }))

  output$showchat <- renderText({
    if(rv.chat$show.chat){
      "Your message"
    } else{
      "hidded"
    }
  })

  outputOptions(output, "showchat", suspendWhenHidden = FALSE)

}

# Modularized app
library(shiny)

ui <- fluidPage(
  chabotUI("hello")
)

server <- function(input, output,session) {
  callModule(chabot, "hello")
}

shinyApp(ui, server)

#3

That was only fixed very recently, you’re lucky you didn’t ask this question a few months ago :slight_smile: