Communication between modules in R shiny when using shiny.router

My shiny app is set up so that each page is a separate module that is called by a router when needed. Two of these modules/pages need to communicate between each other so that when users input their username and password on one page, it also fills in on the other page. (Preferably this would be two way but honestly at this point I'll take anything)

Here's a simplified version of the code I am using.

#global
library(readxl)
library(ggplot2)
library(tidyverse)
library(shiny)
library(rlang)
library(V8)
library(shinydashboard)
library(shiny.router)
library(shinyjs)
library(lubridate)
library(zoo)
library(qicharts2)
library(shinyWidgets)
library(DT)
library(kableExtra)

headerLinks <<- list(
  #"splash" = "splash",
  "Summary of Recording" = "summary",
  "Indicator Detail" = "detail"
)

routes <- c(
  route("home", pageSplashUI("splash")),
  route("summary", pageSummaryUI("summary")),
  route("detail", pageDetailUI("detail"))
)

router <- make_router(
  routes
)
shinyUI(
  tagList(
    shinyjs::useShinyjs(),
    
    router$ui
  )
)

  shinyServer(function(input, output, session) {
  
  moduleControl <- reactiveValues(
    currentPage=NULL,
    moduleMsg=NULL
  )
  
  
  moduleControl$currentPage <- eventReactive(get_page(), {
    get_page()  
  })
  
  pass <- callModule(pageSummary, "summary", "summary", moduleControl)
  callModule(pageDetail, "detail", "detail", moduleControl, pass)
  
  modules <- list(callModule(pageSplash, "home", "splash", moduleControl),
                  callModule(pageDetail, "detail", "detail", moduleControl, pass),
                  callModule(pageSummary, "summary", "summary", moduleControl))
  
  router$server(input,output,session)
})
pageSummaryUI <- function(id) {
  ns <- NS(id)
  
  createFlexSidebarPage(ns("summary"),
                        createFilterGroup(ns("filters"),
                                          createFilterPanel(ns("DetailUP"),
                                                            "Enter Username and Password",
                                                            textInput(ns("Username"),"Username:"),
                                                            textInput(ns("Password"),"Password:"),
                                                            actionButton(ns("Submit"),"Submit", icon("paper-plane"), 
                                                                         style="color: #fff; background-color: #23356a; border-color: #fff")),
                                          
                                          createFilterPanel(ns("Filters"),
                                                            "Filters",
                                                            selectInput(ns("Ethnicity"), "Ethnicity",
                                                                        choices=unique(data$Prioritised.Ethnicities), selected = "All ethnicities"),
                                                            selectInput(ns("Age"), "Age",
                                                                        choices=unique(data$agegroup), selected = "All ages"),
                                                            selectInput(ns("Year"), "Year",
                                                                        choices=unique(data$year), selected = "All years")
                                          )
                        
                        ),
                        "Summary of Recording",
                        T
  )
}
pageSummary <- function(input, output, session, name, moduleControl) {
  ns <- session$ns
  
  observeEvent(input[["route_summary"]], {
    change_page("summary", session = session)
  })
  
  ### ---- username and password reactivity
  
  passvals <- reactiveValues()
  observe({passvals$pass1 <- input$Password})
  observe({passvals$user1 <- input$Username})
  
  return(passvals)

}

pageDetailUI <- function(id) {
  ns <- NS(id)
  
  createFlexSidebarPage(ns("detail"),
                        createFilterGroup(ns("filters"),
                                         createFilterPanel(ns("DetailUP"),
                                                            "Enter Username and Password",
                                                            textInput(ns("Username"),"Username:"),
                                                                      textInput(ns("Password"),"Password:"),
                                                                      actionButton(ns("Submit"),"Submit", icon("paper-plane"), 
                                                                                   style="color: #fff; background-color: #23356a; border-color: #fff")),
                                          createFilterPanel(ns("Filters"),
                                                            "Filters",
                                                            selectInput(ns("Indicator"), "Indicator",
                                                                        choices=unique(dfFinal_G24$Indicator)),
                                                            selectInput(ns("Ethnicity"), "Ethnicity",
                                                                        choices=unique(dfFinal_G24$Prioritised.Ethnicities), selected = "All ethnicities"),
                                                            selectInput(ns("Age"), "Age",
                                                                        choices=unique(dfFinal_G24$agegroup), selected = "All ages"),
                                                            selectInput(ns("Year"), "Year",
                                                                        choices=unique(dfFinal_G24$year), selected = "All years")
                                          )
                        
                        ),
                        "Indicator Detail",
                        T
  )
}

pageDetail <- function(input, output, session, name, moduleControl, pass) {
  ns <- session$ns
  
  observeEvent(input[["route_detail"]], {
    change_page("detail", session = session)
  })
  
  ### ---- username and password reactivity
  
  reactive({
    updateTextInput(pageSummary, "Password", value = pass$pass1)
    })

  
}

Does anyone know how to make this work?

Also asked on stackoverflow

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.