请检查以下内容(我只是修改了您的 if 条件):
library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
shinyApp(
ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
id = "tabactive",
tabPanel("Summary"),
tabPanel("Available funds",
tabsetPanel(
id="tabA",
type = "tabs",
tabPanel("Plot"),
tabPanel("Plot2"))
))
),
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
)
})
observe({
if (input$tabactive == "Available funds" && input$tabA == "Plot"){
golem::invoke_js("showid", "sl")
} else {
golem::invoke_js("hideid", "sl")
}
})
}
)
顺便提一句。你不需要那个 JS 代码来获取input$tabactive
since navbarPage
还有一个id
争论。
另一种方法是包裹uiOutput("sl")
in conditionalPanel
像这样:
library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
shinyApp(
ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
id = "tabactive",
tabPanel("Summary"),
tabPanel("Available funds",
tabsetPanel(
id="tabA",
type = "tabs",
tabPanel("Plot"),
tabPanel("Plot2"))
))
),
rightsidebar = rightSidebar(
background = "dark",
rightSidebarTabContent(
id = 1,
title = "Tab 1",
icon = "desktop",
active = TRUE,
conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot'", {uiOutput("sl")})
)
),
title = "Right Sidebar"
)),
server = function(input, output) {
output$sl <- renderUI({
sliderInput(
"obs",
"Number of observations:",
min = 0, max = 1000, value = 500
)
})
}
)
OP编辑后:
library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
shinyApp(
ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
id = "tabactive",
tabPanel("Summary",
tabsetPanel(
id="tabB",
type = "tabs",
tabPanel("Plot3"),
tabPanel("Plot4"))),
tabPanel("Available funds",
tabsetPanel(
id="tabA",
type = "tabs",
tabPanel("Plot"),
tabPanel("Plot2"))
))
),
rightsidebar = rightSidebar(
background = "dark",
rightSidebarTabContent(
id = 1,
title = "Tab 1",
icon = "desktop",
active = TRUE,
uiOutput("sl"),
uiOutput("sl2"),
uiOutput("sl3"),
uiOutput("sl4")
)
),
title = "Right Sidebar"
)),
server = function(input, output) {
output$sl <- renderUI({
req(input$tabactive)
sliderInput(
"obs",
"Number of observations:",
min = 0, max = 1000, value = 500
)
})
output$sl2 <- renderUI({
req(input$tabactive)
sliderInput(
"obs2",
"Number of observations:",
min = 0, max = 100, value = 50
)
})
output$sl3 <- renderUI({
req(input$tabactive)
sliderInput(
"obs3",
"Number of observations:",
min = 0, max = 100, value = 50
)
})
output$sl4 <- renderUI({
req(input$tabactive)
sliderInput(
"obs4",
"Number of observations:",
min = 0, max = 100, value = 50
)
})
observe({
if (input$tabactive == "Available funds" && input$tabA == "Plot"){
golem::invoke_js("showid", "sl")
} else {
golem::invoke_js("hideid", "sl")
}
})
observe({
if (input$tabactive == "Available funds" && input$tabA == "Plot2"){
golem::invoke_js("showid", "sl2")
} else {
golem::invoke_js("hideid", "sl2")
}
})
observe({
if (input$tabactive == "Summary" && input$tabB == "Plot3"){
golem::invoke_js("showid", "sl3")
} else {
golem::invoke_js("hideid", "sl3")
}
})
observe({
if (input$tabactive == "Summary" && input$tabB == "Plot4"){
golem::invoke_js("showid", "sl4")
} else {
golem::invoke_js("hideid", "sl4")
}
})
}
)
这里有一个UI
-唯一的解决方案:
library(golem)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
shinyApp(
ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
body = dashboardBody(
golem::activate_js(),
navbarPage("Navbar!",
id = "tabactive",
tabPanel("Summary",
tabsetPanel(
id="tabB",
type = "tabs",
tabPanel("Plot3"),
tabPanel("Plot4"))),
tabPanel("Available funds",
tabsetPanel(
id="tabA",
type = "tabs",
tabPanel("Plot1"),
tabPanel("Plot2"))
))
),
rightsidebar = rightSidebar(
background = "dark",
rightSidebarTabContent(
id = 1,
title = "Tab 1",
icon = "desktop",
active = TRUE,
conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot1'", {
sliderInput(
"obs1",
"Number of observations 1:",
min = 0, max = 1000, value = 500
)}),
conditionalPanel(condition = "input.tabactive == 'Available funds' && input.tabA == 'Plot2'", {
sliderInput(
"obs2",
"Number of observations 2:",
min = 0, max = 100, value = 50
)}),
conditionalPanel(condition = "input.tabactive == 'Summary' && input.tabB == 'Plot3'", {
sliderInput(
"obs3",
"Number of observations 3:",
min = 0, max = 100, value = 50
)}),
conditionalPanel(condition = "input.tabactive == 'Summary' && input.tabB == 'Plot4'", {
sliderInput(
"obs4",
"Number of observations 4:",
min = 0, max = 100, value = 50
)})
)
),
title = "Right Sidebar"
)),
server = function(input, output) {}
)