Skip to content

Commit

Permalink
add question_visibility reactive value to store status of question vi…
Browse files Browse the repository at this point in the history
…sibility
  • Loading branch information
jhelvy committed Oct 4, 2024
1 parent 1ee7982 commit 61a3b83
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,23 @@ sd_server <- function(
# Reactive values storing status of show_if conditions
show_if_results <- set_show_if_conditions(show_if)

# Reactive values storing the visibility state of all questions
question_visibility <- shiny::reactiveVal(
setNames(rep(TRUE, length(question_ids)), question_ids)
)

# Observer to hide/show based on show_if condition results
shiny::observe({
results <- show_if_results()

# Update question visibility based on show_if results
current_visibility <- question_visibility()
for (target in names(results)) {
current_visibility[target] <- results[[target]]
}
question_visibility(current_visibility)

# Show or hide question
for (target in names(results)) {
if (results[[target]]) {
shinyjs::show(target)
Expand Down Expand Up @@ -315,17 +329,13 @@ sd_server <- function(
# Page navigation ----

check_required <- function(page) {
all(vapply(page$required_questions, function(q) {
is_visible <- is_question_visible(q)
!is_visible || check_answer(q, input)
required_questions <- page$required_questions
is_visible <- question_visibility()[required_questions]
all(vapply(required_questions, function(q) {
!is_visible[q] || check_answer(q, input)
}, logical(1)))
}

is_question_visible <- function(q) {
results <- show_if_results()
!q %in% names(results) || results[[q]]
}

# Determine which page is next, then update current_page_id() to it
observe({
lapply(pages, function(page) {
Expand Down

0 comments on commit 61a3b83

Please sign in to comment.