jdtrat / shinysurveys

Develop and deploy surveys in Shiny/R.
https://shinysurveys.jdtrat.com/
Other
95 stars 27 forks source link

How to include showModal and modalDialog when using shinysurveys across multiple tabPanel #45

Open covid19ec opened 2 years ago

covid19ec commented 2 years ago

Hi all. I am using the shinysurveys package to build surveys across multiple tabs. I sketched a code and is working but I would like to include a message that should appear only when the first Tab is clicked or loaded (in this case as Tab1 is showed always first, it should appear at the start of the app and every time Tab1 is clicked). Here is the code for my app:

library(shiny)
library(shinysurveys)
#Questions 1
df1 <- data.frame(question = "What is your favorite food?",
                 option = "Your Answer",
                 input_type = "text",
                 input_id = "favorite_food",
                 dependence = NA,
                 dependence_value = NA,
                 required = F)
#Questions 2
df2 <- data.frame(question = "Where do you live?",
                 option = "Your Answer",
                 input_type = "text",
                 input_id = "city",
                 dependence = NA,
                 dependence_value = NA,
                 required = F)
#App
ui <- fluidPage(
  tabsetPanel(id = "AC",
              tabPanel(tabName = "A1","Tab1",
                       surveyOutput(df = df1,theme='pink',
                                    survey_title = "Hello",
                                    survey_description = "This is page 1.")),
              tabPanel(tabName = "A2","Tab2",
                       surveyOutput(df = df2,theme='pink',
                                    survey_title = "Hello",
                                    survey_description = "This is page 2."))
  )
)
server <- function(input, output, session) {
  observeEvent(input$AC,{ 
    if(input$AC == "A1")
    {
      showModal(modalDialog(
        title = "Hello again!",
        "You are in page 1!",
        easyClose = TRUE
      ))
    } else{}
  }
  )
}
shinyApp(ui,server)

The code works fine but I do not know what I am doing wrong. In order to show a message for the first tab, I have used observeEvent() and created a condition for the id of tabsetPanel() when tabPanel() takes the value of A1 using showModal() and modalDialog() but when I run the app I did not get the message. I get this without the message sketched on showModal():

imagen

I do not if it is possible to add this option. I have looked around trying other packages like shinyBS or bs4Dash but I can not get them working. Also, I found an example with the last package that uses navbar but the message or popover that appears in this app is similar to that I want to implement. Here is the link.

In that page you can see how nice the message appears at the start or when you click the first tab. I wish I could do something similar using showModal() and modalDialog().

Many thanks.