r - 使用dashboardPage时,闪亮应用程序中两个不同tabItems的独立游览
问题描述
我正在使用dashboardPage
构建闪亮应用程序的 UI。在这dashboardPage
我有两个tabItems
(MENU1 和 MENU2)。每一项都执行不同的任务tabItems
,我需要指导用户完成这些任务。为了指导用户,我在下面的代码中actionButton
为每个用户tabItem
添加了相应的内容。observeEvent
为此,我分别使用和introBox
控制了步骤和介绍。data.step
data.intro
不幸的是,这不是一个好的解决方案,因为它们data.steps
是相互关联的,总共需要 2 个步骤。在 MENU1 中,当我单击第一个时,actionButton
我必须完成两个步骤才能完成此游览。在 MENU2 中,当我单击第二个时actionButton
,此游览从 MENU1 的第一步开始。
我想要的是 MENU1 的一次巡演和 MENU2 的另一次巡演。有什么建议么?
这是我目前的解决方案:
library(shiny)
library(shinydashboard)
library(rintrojs)
library(leaflet)
ui<-dashboardPage(
dashboardHeader(title = "shinyApp"),
dashboardSidebar(
sidebarMenu(
menuItem("Menu1", tabName = "MENU1"),
menuItem("Menu2", tabName = "MENU2")
)
),
dashboardBody(
tabItems(
tabItem("MENU1",
fluidRow(
introjsUI(),
column(3,
actionButton("start", "START", class="btn-link")
)#,
),
fluidRow(
column(9,
fluidRow(
column(7,
introBox(
actionButton("button1", "Button1"),
data.step=1,
data.intro = wellPanel(
p("Press here, button1")
)
)
)
)
)
)
),
tabItem("MENU2",
fluidRow(
introjsUI(),
column(2,
actionButton("start2", "START2", class="btn-link")
)
),
fluidRow(
column(7,
introBox(
wellPanel(
leafletOutput("mapLocal", width = "100%", height = 600)
),
data.step=2,
data.intro = wellPanel(
p("Press here, button2")
)
)
)
)
)
)
)
)
server <- function(input, output, session){
observeEvent(input$start,
introjs(session))
observeEvent(input$start2,
introjs(session))
}
shinyApp(ui = ui, server = server)
解决方案
这是一个带有一次游览的解决方案,但是在显示第二部分之前会翻转页面。我求助于一些JavaScript
添加beforechange
事件。在这种情况下,您可以检查是否要显示您的第二步,如果是这种情况,您可以通过模拟单击相应的菜单项来手动触发页面更改。
library(shiny)
library(shinydashboard)
library(rintrojs)
ui<-dashboardPage(
dashboardHeader(title = "shinyApp"),
dashboardSidebar(
sidebarMenu(
menuItem("Menu1", tabName = "MENU1"),
menuItem("Menu2", tabName = "MENU2")
)
),
dashboardBody(
introjsUI(),
tabItems(
tabItem("MENU1",
fluidRow(
actionButton("start", "START", class="btn-link"),
introBox(
actionButton("button1", "Button1"),
data.step = 1,
data.intro = wellPanel(
p("Press here, button1")
)
)
)
),
tabItem("MENU2",
fluidRow(
introBox(
plotOutput("mapLocal", width = "100%", height = 600),
data.step = 2,
data.intro = wellPanel(
p("Plot Area")
)
)
)
)
)
)
)
js <- paste0("if (targetElement.getAttribute('data-step') === '2') {",
"$(\"a[data-value='MENU2']\").trigger('click')",
"}")
server <- function(input, output, session){
observeEvent(input$start, introjs(session, events = list(onbeforechange = I(js))))
}
shinyApp(ui = ui, server = server)
更新
在查看rintrojs的源代码时,我发现有一个rintrojs
元素,这让我很好奇。经过一番搜索,我发现切换窗格任务是其创建者 rintrojs
已经预见到的。因此,上面的代码可以进一步简化(并且更健壮,因为我们不必手动检查选项卡):
server <- function(input, output, session){
observeEvent(input$start, introjs(session,
events = list(onbeforechange = readCallback("switchTabs"))))
}
推荐阅读
- html - 修改 jm 微调器点以使其大小相同而没有反弹
- eclipse - 如何通过命令行重新启动或退出我的 Eclipse
- lua - 为什么使用 scipy 从 Lua 火炬和 pytorch 加载 .mat 文件后矩阵之间存在差异?
- python - Dask:在列上删除 NA?
- php - Symfony 中的注解机制——它是如何工作的?
- android-studio - Kotlin REPL:由于可能的重新分配,禁止捕获的成员值初始化
- c# - 通过 OLEDB 连接到 MySQL
- php - 在使用 PHP 中的 IF 语句更新 php 中的表格内容之前如何检查?
- c# - 为什么尝试为 MySql 创建 using 指令会产生“缺少 using 指令”错误?
- r - 在 r 中绘制没有谷歌地图 API 的城市地图