简体   繁体   English

使用 shiny.router 时 R shiny 中的模块之间的通信

[英]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.我的 shiny 应用程序设置为每个页面都是一个单独的模块,在需要时由路由器调用。 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

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(),
    tags$head(
      tags$link(rel = "stylesheet", type = "text/css", href = "css/custom.css")
    ),
    
    router$ui,
    tags$script(src="js/util.js")
  )
)

  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?有谁知道如何使这项工作?

  1. Call the module for the page you are switching to in your server and store the session.在服务器中调用您要切换到的页面的模块并存储 session。
  2. Use that session in the updateTextInput call.在 updateTextInput 调用中使用该 session 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM