Consider this multi-pages app.
library(shiny)
library(bslib)
shinyApp(
ui = page_fluid(
navset_bar(
nav_panel(
"Foo",
page_sidebar(
sidebar = sidebar(
textInput("foo", "Foo field", value = NULL),
conditionalPanel(
condition = "input.blah === true",
numericInput(
"foo-num",
"Foo numeric",
value = 1,
min = 1
)
)
)
)
),
nav_panel(
"Bar",
page_sidebar(
sidebar = sidebar(
textInput("bar", "Bar field", value = NULL),
conditionalPanel(
condition = "input.blah === true",
numericInput(
"bar-num",
"Bar numeric",
value = 1,
min = 1
)
)
)
)
)
)
),
server = function(input, output, session) {}
)
I'd like to have a switch button in the sidebar shared across all pages.
Currently, solutions I can think of to do this include:
-
A global
bslib::input_switch()
button. -
Multiple switch buttons (one in every page) that inherit the value of each other from page to page.
Solution #1 is the easiest to implement but it has to be outside the main sidebar (for example in bslib::navset_bar(header =)
). Don't know how I could have it as if it was inside bslib::sidebar()
though.
Solution #2 makes it easy to insert the button exactly where I want but I can't think of a simple way to inherit its value across all pages because the function doesn't seem to return the actual value of the button. It's probably possible to pass reactive values along every button but it's not very convenient.
Inserting multiple button with:
insertUI(
selector = ".sidebar-content",
ui = input_switch("blah", "Blah", value = FALSE),
where = "afterBegin",
immediate = TRUE,
multiple = TRUE
)
Seems to work but buttons are not updated across pages (will be displayed as if turned off although they're turned on).
Any idea of a simple way to do this?