I'm looking to stop a dropdownbutton
(shinywidgets
) from opening when the button is clicked based on a condition. This to avoid renderUI
errors on missing input
for content on the dropdownButton
modal panel.
When a user clicks on a dropdownButton
, it normally opens a panel. In my case, this panel contains renderUI
elements that depend on various variables.
If these variables do not exist yet, the renderUIs
will cause errors to spit out.
What I would like to know is whether there is a way to look at the click
observeEvent(input$MydropdownButton, { ....})
and then completely stop it from opening the panel if a condition is not met, rather than toggle it to close immediately (not working version)
What I plan to do, is to give the user a sweetalert
instead that informs the user of which options he has to create or load the needed data. And I know how to do the message, purely looking to stop the opening part in an 'if else' way
- I know I can use
shinyjs::disable('MydropdownButton')
inside an observer withif
statement to block the use of the button, but this would not allow me to trigger thesweetalert
on a click anymore - I also know I can adjust all my renderUIs not to render if the needed input is missing, but by now there are a lot of
renderUIs
involved, and I'm:- A: afraid to make a mess of the code, and
- B: eager to find out if there is a way in general to stop the opening of
dropdownButtons
I've tried something like this:
observeEvent(input$MydropdownButton, {
if(!is.null(values$neededData)) { 'just open the dropdownbutton' }
else { toggleDropdownButton('TestDrop')
'run sweetalert code'}
})
But the toggleDropdownButton
will only close the dropdownButton
panel once it's already triggered to open, and thus shiny tried to render
the ui
element, with the resulting error, rather than block it from opening.
Here are a full server
and ui
code files to demonstrate it calling for non-existing numbers.
SERVER file
shinyServer = function(input, output, session) {
values <- reactiveValues()
output$Reset_Threshold <- renderUI({
if(values$randomNr == 2) { actionButton(inputId = "Reset_Threshold", label = icon("undo")) }
else if(values$randomNr == 1) { actionButton(inputId = "Reset_Threshold", label = icon("table")) }
})
observeEvent(input$TestDrop, {
if(!is.null(values$randomNr )) { print('no problems')}
else { toggleDropdownButton('TestDrop')
# Run other code here to alert user.
}
})
}
UI file
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
dropdownButton(inputId= "TestDrop",
uiOutput('Reset_Threshold'),
icon = icon("table"), tooltip = tooltipOptions(title = "Click"))
)
```