Last active
June 21, 2021 07:51
-
-
Save Gotfrid/29e72748e8ef0b07c2776598f8bfae97 to your computer and use it in GitHub Desktop.
Minimal reproducible example to test reactivity of selectInput with updateSelectInput
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app prototype ----------------------------------------------------------- | |
library(shiny) | |
# define select chioces | |
input_choices <- data.frame( | |
x = c(1, 1, 2, 2, 3, 3), | |
y = c(11, 12, 21, 22, 31, 32) | |
) | |
# choices for x | |
x_choices <- unique(input_choices$x) | |
ui <- shinyUI( | |
fluidPage( | |
tagList( | |
selectInput("x_selection", "X", x_choices), | |
p(), | |
selectInput("y_selection", "Y", c("")) | |
) | |
) | |
) | |
server <- shinyServer( | |
function(input, output, session) { | |
observeEvent(input$x_selection, { | |
y_choices <- | |
input_choices[ | |
input_choices$x == input$x_selection, | |
"y" | |
] | |
updateSelectInput( | |
session, | |
"y_selection", | |
choices = y_choices | |
) | |
}) | |
} | |
) | |
shinyApp(ui, server) | |
# testing (run separately) ------------------------------------------------ | |
# | |
# library(testthat) | |
# library(shinytest) | |
# | |
# test_that("Second selectInput is reactive", { | |
# testServer(app = application, { | |
# # init application | |
# app <- ShinyDriver$new(path = "minimal_example.R") | |
# | |
# # check startup values | |
# expect_equal(app$getValue("x_selection"), "1") | |
# expect_equal(app$getValue("y_selection"), "11") | |
# | |
# # change x_selection input | |
# app$setValue("x_selection", "2") | |
# | |
# # expect things to change | |
# expect_equal(app$getValue("x_selection"), "2") | |
# expect_equal(app$getValue("y_selection"), "21") | |
# | |
# # stop & remove app | |
# app$finalize(); rm(app) | |
# }) | |
# }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment