Skip to content

Commit

Permalink
bug fix: check_skip_show now accounts for reactive question ids
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Oct 4, 2024
1 parent ae45188 commit 1ee7982
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# surveydown (development version)

- Bug fix: reactive question ids (those defined in the server) were not being considered in `check_skip_show()` checks, so you'd get an error that the question id was invalid.

# surveydown 0.3.3

- `sd_server()` now has a new parameter called `auto_scroll`. It's default to `TRUE`, which enables auto scrolling that tracks the user's input, can be turned off by changing to `FALSE`. Thanks to the contribution from [Zain Hoda](https://github.com/zainhoda1).
Expand Down
12 changes: 11 additions & 1 deletion R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ get_question_structure <- function(html_content) {
return(question_structure)
}

get_output_ids <- function() {
output <- shiny::getDefaultReactiveDomain()$output
outs <- outputOptions(output)
return(names(outs))
}

check_skip_show <- function(
question_ids, question_values, page_ids, skip_if, show_if
) {
Expand All @@ -189,7 +195,11 @@ check_skip_show <- function(
}

if (!is.null(show_if)) {
invalid_show_targets <- setdiff(show_if$targets, question_ids)
# Get any potential question_ids from the output
invalid_show_targets <- setdiff(
show_if$targets,
c(question_ids, get_output_ids())
)
if (length(invalid_show_targets) > 0) {
stop(sprintf(
"Invalid show_if targets: %s. These must be question IDs defined in the survey.qmd file.",
Expand Down
44 changes: 22 additions & 22 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,9 @@ sd_server <- function(
show_if
)

# Set up show_if conditions
show_if_results <- if (!is.null(show_if)) {
set_show_if_conditions(show_if)
} else {
shiny::reactive(list())
}
# Create an observer to handle visibility
shiny::observe({
results <- show_if_results()
for (target in names(results)) {
if (results[[target]]) {
shinyjs::show(target)
} else {
shinyjs::hide(target)
}
}
})

# Initialize local variables ----

# Check if db is NULL (either left blank or specified with ignore = TRUE)
# Check if db is NULL (either blank or specified with ignore = TRUE)
ignore_mode <- is.null(db)

# Create local objects from config file
Expand All @@ -155,6 +137,23 @@ sd_server <- function(
start_page_ts_id <- page_ts_ids[which(page_ids == start_page)]
all_ids <- c('time_end', question_ids, question_ts_ids, page_ts_ids)

# Set up show_if conditions ----

# Reactive values storing status of show_if conditions
show_if_results <- set_show_if_conditions(show_if)

# Observer to hide/show based on show_if condition results
shiny::observe({
results <- show_if_results()
for (target in names(results)) {
if (results[[target]]) {
shinyjs::show(target)
} else {
shinyjs::hide(target)
}
}
})

# Initialize local functions ----

# Function to update progress bar
Expand Down Expand Up @@ -456,11 +455,12 @@ sd_show_if <- function(...) {
}

set_show_if_conditions <- function(show_if) {

if (is.null(show_if)) { shiny::reactive(list()) }

conditions <- show_if$conditions

if (length(conditions) == 0) {
return(shiny::reactive(list()))
}
if (length(conditions) == 0) { shiny::reactive(list()) }

# Group conditions by target
grouped_conditions <- split(conditions, sapply(conditions, function(rule) rule$target))
Expand Down

0 comments on commit 1ee7982

Please sign in to comment.