gpt4 book ai didi

javascript - 首次加载应用程序时,根据点击事件显示小部件不起作用

转载 作者:行者123 更新时间:2023-12-02 22:35:28 24 4
gpt4 key购买 nike

我下面有一个 Shiny 的仪表板,其中右侧边栏的小部件应该仅在“Plot”选项卡面板处于事件状态时显示,并且除了第一次加载应用程序之外实际上会发生这种情况当小部件也显示在'Summary' 选项卡面板中时。我知道发生这种情况是因为激活是通过单击选项卡面板名称来进行的。但我该如何解决这个问题呢?

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
tabPanel("Summary"

),
tabPanel("Plot"
)),
tags$script(
'$("a[data-toggle=\'tab\']").click(function(){
Shiny.setInputValue("tabactive", $(this).data("value"))
})'
)
),
rightsidebar = rightSidebar(
background = "dark",
rightSidebarTabContent(
id = 1,
title = "Tab 1",
icon = "desktop",
active = TRUE,
uiOutput("sl")

)

),
title = "Right Sidebar"
),
server = function(input, output) {
output$sl<-renderUI({
sliderInput(
"obs",
"Number of observations:",
min = 0, max = 1000, value = 500
)
})

observeEvent( input$tabactive , {
if (input$tabactive == "Plot"){
golem::invoke_js("showid", "sl")
} else {
golem::invoke_js("hideid", "sl")
}
})



}
)

最佳答案

您可以通过将 req(input$tabactive) 添加到 renderUI 调用来抑制初始渲染:

library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
tabPanel("Summary"

),
tabPanel("Plot"
)),
tags$script(
'$("a[data-toggle=\'tab\']").click(function(){
Shiny.setInputValue("tabactive", $(this).data("value"))
})'
)
),
rightsidebar = rightSidebar(
background = "dark",
rightSidebarTabContent(
id = 1,
title = "Tab 1",
icon = "desktop",
active = TRUE,
uiOutput("sl")

)

),
title = "Right Sidebar"
),
server = function(input, output) {
output$sl <- renderUI({
req(input$tabactive)
sliderInput(
"obs",
"Number of observations:",
min = 0, max = 1000, value = 500
)
})

observeEvent( input$tabactive , {
if (input$tabactive == "Plot"){
golem::invoke_js("showid", "sl")
} else {
golem::invoke_js("hideid", "sl")
}
})
}
)

关于javascript - 首次加载应用程序时,根据点击事件显示小部件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58759396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com