Javascript 当使用不同选项卡的直接链接打开ShinyDashboard时,侧边栏中的menuSubItem未激活
在下面的代码中,当使用第一个选项卡中的“计算完成”链接打开菜单项时,我无法激活该菜单项。该链接会打开正确的选项卡,但无法自动激活/打开侧边栏中的关联子菜单 代码是根据这里的示例修改的Javascript 当使用不同选项卡的直接链接打开ShinyDashboard时,侧边栏中的menuSubItem未激活,javascript,html,r,shiny,shinydashboard,Javascript,Html,R,Shiny,Shinydashboard,在下面的代码中,当使用第一个选项卡中的“计算完成”链接打开菜单项时,我无法激活该菜单项。该链接会打开正确的选项卡,但无法自动激活/打开侧边栏中的关联子菜单 代码是根据这里的示例修改的 库(闪亮) 图书馆(shinydashboard) 欢迎来到stackoverflow 您可以为菜单项的“结果”提供id,并动态更改其显示样式 请使用库(shinyjs)检查我的方法: 库(闪亮) 图书馆(shinydashboard) 图书馆(shinyjs) 欢迎来到stackoverflow 您可以为菜单项的
库(闪亮)
图书馆(shinydashboard)
欢迎来到stackoverflow
您可以为菜单项
的“结果”提供id
,并动态更改其显示样式
请使用库(shinyjs)
检查我的方法:
库(闪亮)
图书馆(shinydashboard)
图书馆(shinyjs)
欢迎来到stackoverflow
您可以为菜单项
的“结果”提供id
,并动态更改其显示样式
请使用库(shinyjs)
检查我的方法:
库(闪亮)
图书馆(shinydashboard)
图书馆(shinyjs)
请检查我的答案。非常感谢您抽出时间回答这个问题,@ismirsehregal。它工作得很好!请检查我的答案。非常感谢您抽出时间回答这个问题,@ismirsehregal。它工作得很好!解决方案按预期工作,@ismirsehegal。谢谢解决方案按预期工作,@ismirsehegal。谢谢
library(shiny)
library(shinydashboard)
ui <- shinyUI(
dashboardPage(
dashboardHeader(title = "Some Header"),
dashboardSidebar(
sidebarMenu(
menuItem("Computations", tabName = "tabItem1", icon = icon("dashboard")),
menuItem("Results", tabName = "tabItem2", icon = icon("th"),
menuSubItem("Test", tabName = "subitem2"))
)
),
dashboardBody(
tags$script(HTML("
var openTab = function(tabName){
$('a', $('.sidebar')).each(function() {
if(this.getAttribute('data-value') == tabName) {
this.click()
};
});
}
")),
tabItems(
tabItem(tabName = "tabItem1",
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
),
infoBoxOutput("out1")
),
tabItem(tabName = "subitem2",
h2("Widgets tab content")
)
)
)
)
)
server <- function(input, output){
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
output$out1 <- renderInfoBox({
infoBox("Completed",
a("Computation Completed", onclick = "openTab('subitem2')", href="#"),
icon = icon("thumbs-o-up"), color = "green"
)
})
}
shinyApp(ui, server)
library(shiny)
library(shinydashboard)
library(shinyjs)
jsCode <- 'shinyjs.hidemenuItem = function(targetid) {var x = document.getElementById(targetid); x.style.display = "none"; x.classList.remove("menu-open");};
shinyjs.showmenuItem = function(targetid) {var x = document.getElementById(targetid); x.style.display = "block"; x.classList.add("menu-open");};'
ui <- shinyUI(
dashboardPage(
dashboardHeader(title = "Some Header"),
dashboardSidebar(
sidebarMenu(
id = "sidebarID",
menuItem("Computations", tabName = "tabItem1", icon = icon("dashboard")),
menuItem(text = "Results", id = "resultsID", tabName = "tabItem2", icon = icon("th"),
menuSubItem("Test", tabName = "subitem2"))
)
),
dashboardBody(
useShinyjs(),
extendShinyjs(text = jsCode),
tabItems(
tabItem(tabName = "tabItem1",
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
),
infoBoxOutput("out1")
),
tabItem(tabName = "subitem2",
h2("Widgets tab content")
)
)
)
)
)
server <- function(input, output, session){
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
output$out1 <- renderInfoBox({
infoBox("Completed",
actionLink(inputId = "completed", label = "Computation Completed"),
icon = icon("thumbs-o-up"), color = "green"
)
})
observeEvent(input$completed, {
js$showmenuItem("resultsID")
updateTabItems(session, inputId="sidebarID", selected = "subitem2")
})
observeEvent(input$sidebarID, {
if(input$sidebarID != "subitem2"){
js$hidemenuItem("resultsID")
}
})
}
shinyApp(ui, server)