我正在开发一个 Rshiny 应用程序,我希望
bslib::tooltip
有条件地出现在某些选项卡上。我已经只在所需条件下显示文本了:
但在其他条件下我仍然会出现黑色指针和容器空间:
appR:
library(bslib) # bootstrap theme
library(shiny) # runs the show
# real app uses modules
sidebarSel_ui <- function(id){
ns <- NS(id)
# conditional choices based on tab
choiceTxt <- if (id=="pg1") {
c("Choice 1","Choice 2","Choice 3","Choice 4")
} else if (id=="pg2") {
c("Choice 1","Choice 2")
}
# conditional tooltip based on tab
tipTxt <- if (id=="pg1") {
"Some info about choices 3 and 4"
} else {
NULL # leaves black pointer and container space
}
# send to UI
tagList(
selectInput(
inputId=ns("select1"),
label="Some choices",
choices=choiceTxt,
selectize=FALSE,
selected="Choice 1"
) |>
tooltip(tipTxt, placement="right") # looks weird on tab 2
)
}
ui <- page_fluid(
# call CSS theme
theme=bs_theme(bootswatch="cosmo"),
sidebarLayout(
sidebarPanel(
width=2,
conditionalPanel(
condition="input.condPanel==1",
sidebarSel_ui("pg1")
),
conditionalPanel(
condition="input.condPanel==2",
sidebarSel_ui("pg2")
)
),
mainPanel(
width=10,
tabsetPanel(
id="condPanel",
tabPanel(
title="Tab 1",
value=1
),
tabPanel(
title="Tab 2",
value=2
)
)
)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
我尝试将工具提示放入
conditionalPanel
中,但它不起作用,因为工具提示是在 selectInput
之后通过管道传输的 - 输入是 actually 工具提示的第一个参数(因此在以下情况下将其包装在工具提示 div 中)渲染),所以我不知道如何使工具提示成为条件,同时保留其余输入。当我不希望出现工具提示时,我可以做些什么来摆脱黑色指针和空容器吗?
仅在
id == "pg1"
时添加工具提示。 SidebarSel_ui()
现在变成:
sidebarSel_ui <- function(id) {
ns <- NS(id)
# conditional choices based on tab
choiceTxt <- if (id == "pg1") {
c("Choice 1", "Choice 2", "Choice 3", "Choice 4")
} else if (id == "pg2") {
c("Choice 1", "Choice 2")
}
input <- selectInput(
inputId = ns("select1"),
label = "Some choices",
choices = choiceTxt,
selectize = FALSE,
selected = "Choice 1"
)
if (id == "pg1") {
tipTxt <- "Some info about choices 3 and 4"
input <- input |> tooltip(tipTxt, placement = "right")
}
input
}