r - 使用 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)
})
}
有谁知道如何使这项工作?
解决方案
- 在服务器中调用您要切换到的页面的模块并存储会话。
- 在 updateTextInput 调用中使用该会话。
推荐阅读
- javascript - 如何使用 AJAX 创建服务器端目录列表?
- amazon-web-services - 在现有 AWS SageMaker Ground Truth 标记作业上启用自动标记
- c# - 展平表单字段后隐藏在 PDF 戳后面的文本内容 - iTextSharp,Adobe
- php - 使用 php 获取条带表单数据
- css - ASP、CSS 和 VS2019
- c++ - 选择具有 UUID 类型的列在 C++ 中返回语法错误
- c# - 我可以知道防火墙命令行来阻止程序访问某些 IP 地址吗?
- azure - 运行 GitHub 工作流时收到错误
- powerbi - SSO 能否用于创建将驻留在 PBI 服务中的数据流?
- plotly - 使用破折号绘制带有下拉菜单的 y 轴