Reputation: 1
In my shiny application in the ui function I defined object uiOutput with id "Filter" and in my server function I would like to update this object in dependence on the value of reactiveValues -> test$Value. Right now in the observeEvent like a first is execuded the line with the update of test$Value to False and then the output$Filter is loaded.
How to change the code to load renderUI() with reactive value test$Value with TRUE and then update this value to FALSE.
test = reactiveValues()
test$Value = TRUE
observeEvent(input$X,{
output$Filter<- renderUI({
if(test$Value){
# some code
} else {
# some code
}
})
test$Value = FALSE
})
Upvotes: 0
Views: 268
Reputation: 1280
I was told at one point, and I'm not entirely certain why, but that it's generally best practice to not have outputs within observeEvents. Typically, whatever is accomplished that way can be done outside of it.
Using your basic idea, I made a simple app that helps to demonstrate how changing a selectInput can be observed to update the reactiveValues. Then the renderUI will change based on the reactiveValues change. I hope this helps you out!
library(shiny)
ui <- fluidPage(
selectInput("X", "Select", choices = c(T,F)),
uiOutput("Filter")
)
server <- function(input, output, session) {
test = reactiveValues()
test$Value = TRUE
observeEvent(input$X, {
test$Value<-input$X
})
output$Filter<-renderUI({
if(test$Value == T) {
h3("Hello")
} else {
"Goodbye"
}
})
}
shinyApp(ui, server)
Upvotes: 0