首页 > 解决方案 > 使用 shiny.router 时 R 中的模块之间的通信闪亮

问题描述

我的闪亮应用程序设置为每个页面都是一个单独的模块,在需要时由路由器调用。这些模块/页面中的两个需要相互通信,以便当用户在一个页面上输入他们的用户名和密码时,它也会在另一个页面上填写。(最好这是两种方式,但老实说,在这一点上我会采取任何措施)

这是我正在使用的代码的简化版本。

#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)
    })

  
}

有谁知道如何使这项工作?

标签: rshinymodulereactive

解决方案


  1. 在服务器中调用您要切换到的页面的模块并存储会话。
  2. 在 updateTextInput 调用中使用该会话。

推荐阅读