Reputation: 1036
I have two overlapping observeEvents. By default dropdown value of test2
input id gets updated with 3 times of value in test1
inputid. If button is clicked, I want test1
to be updated with no. of clicks, accordingly dropdown value of test2
input id to be updated with 2 times (instead of 3) of value in test1
inputid.
Issue is as soon as test1
is triggered, it goes to observeEvent(input$test1, { })
and ignore update of test2
with 2 times.
Is there any way to tell shiny to ignore observeEvent(input$test1, { })
when btntest
event is triggered.
ui <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
fluidRow(
column(width = 4,
sliderInput("obs",
"Number of observations:",
min = 0,
max = 1000,
value = 500),
selectizeInput("test1",
label = "Dropdown",
choices = 1:10),
selectizeInput("test2",
label = "Dropdown2",
choices = NULL),
actionButton("btntest", "Button1")
)
)
)
# Server logic
server <- function(input, output, session) {
observeEvent(input$test1, {
updateSelectizeInput(session, "test2",
choices = 3*as.numeric(input$test1))
})
myval <- reactiveValues()
myval$count <- 0
observeEvent(input$btntest, {
myval$count <- myval$count + 1
updateSelectizeInput(session, "test1",
choices = 1:10,
selected = myval$count
)
updateSelectizeInput(session, "test2",
choices = myval$count * 2
)
})
}
# Complete app with UI and server components
shinyApp(ui, server)
Upvotes: 1
Views: 80
Reputation: 33407
You'll need to distinguish between updates triggered by the user and indirect updates. This can be done using a reactiveVal
:
library(shiny)
ui <- fluidPage(titlePanel("Hello Shiny!"),
fluidRow(
column(
width = 4,
sliderInput(
"obs",
"Number of observations:",
min = 0,
max = 1000,
value = 500
),
selectizeInput("test1",
label = "Dropdown",
choices = 1:10),
selectizeInput("test2",
label = "Dropdown2",
choices = NULL),
actionButton("btntest", "Button1")
)
))
server <- function(input, output, session) {
indirectUpdate <- reactiveVal(FALSE)
observeEvent(input$test1, {
if(!indirectUpdate()){
updateSelectizeInput(session, "test2",
choices = 3 * as.numeric(input$test1))
}
indirectUpdate(FALSE)
})
observeEvent(input$btntest, {
indirectUpdate(TRUE)
updateSelectizeInput(session,
"test1",
choices = 1:10,
selected = input$btntest)
updateSelectizeInput(session, "test2",
choices = input$btntest * 2)
})
}
shinyApp(ui, server)
Upvotes: 2