r - 如何使用按钮更改 R 闪亮应用程序中的页面
问题描述
我正在尝试使用按钮来更改闪亮应用程序中的页面。我发现了像这样的例子,这些例子看起来很简单,但由于某种原因,我无法让它发挥作用。下面是我在app.R
文件中创建的可重现示例。这将创建一个双页应用程序,在第一页上有一个按钮,但单击该按钮不会将您移动到第二页。任何提示将非常感谢。
pageButtonUi <- function(id) {
actionButton(NS(id, "page_change"),
label="Change the Page")
}
pageButtonServer <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$page_change, {
updateNavbarPage(session=session,
inputId="pages",
selected="second_page")
})
})
}
ui <- navbarPage(
title="test",
id="pages",
tabPanel(title="first page",
sidebarLayout(
sidebarPanel(
pageButtonUi("page")
),
mainPanel(
)
)
),
tabPanel(title="second_page", "second_page")
)
server <- function(input, output, session) {
pageButtonServer("page")
}
shinyApp(ui, server)
解决方案
您正在使用闪亮的模块。tabPanel 是在顶级 UI 中定义的,但您正在尝试使用较低级别(模块)服务器来更新顶级 UI。这行不通。因此,您需要使用顶级服务器来更新顶级 UI。换句话说,您需要将父session
对象传递给您的模块。
这是解决方法:
library(shiny)
pageButtonUi <- function(id) {
actionButton(NS(id, "page_change"),
label="Change the Page")
}
pageButtonServer <- function(id, parentSession) {
moduleServer(id, function(input, output, session) {
observeEvent(input$page_change, {
updateNavbarPage(session=parentSession,
inputId="pages",
selected="second_page")
})
})
}
ui <- navbarPage(
title="test",
id="pages",
tabPanel(title="first page",
sidebarLayout(
sidebarPanel(
pageButtonUi("page")
),
mainPanel(
)
)
),
tabPanel(title="second_page", "second_page")
)
server <- function(input, output, session) {
pageButtonServer("page", parentSession = session)
}
shinyApp(ui, server)
即使对于高级用户来说,这也不容易理解。尝试阅读 Rstudio 文章,看看他们如何定义session
会有帮助。
推荐阅读
- laravel - 错误 api post vue laravel
- powerbi - 瀑布图 - Power Bi
- apple-push-notifications - APNS 内容可用推送在 iOS 15 中不起作用
- c# - StreamReader ReadLineAsync 忽略回车并挂起
- javascript - 装饰 textarea 后 CodeMirror 样式不起作用
- android - 你能给我一些示例代码来证明永远不要依赖于执行可组合函数的副作用吗?
- javascript - 如何使用 ffmpeg 模块修剪 youtube 视频?
- typescript - 具有默认参数的函数的 JS Doc
- javascript - 有没有办法将值动态添加到 onclick 函数?
- android - 了解系统包和意图的包可见性过滤器