Skip to content

Commit

Permalink
Moved sd_store_value() and sd_copy_value() to the server.R
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Aug 23, 2024
1 parent 4af09c8 commit 74aa206
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 73 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# surveydown (development version)

- Moved `sd_store_value()` and `sd_copy_value()` to the server.R file (these are server operations).

# surveydown 0.1.2

- Moved the main surveydown.lua file into the package at inst/quarto/filters. The surveydown Quarto extension is now just a wrapper to load this file, so the extension won't likely need to update any further as all updates can be made in the package lua file.
- Changed `jhelvy` to `surveydown-dev` in version control functions.
- Now if there are duplicated `page_id` and `question_id`, the survey will stop and show error.
- Removed roxygen2 documentation from non-exported functions.

# surveydown 0.1.1

Expand Down
73 changes: 73 additions & 0 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,76 @@ sd_set_password <- function(password) {

message("Password set successfully and .Renviron added to .gitignore.")
}

#' Store a value
#'
#' This function allows storing additional values to be included in the survey data,
#' such as respondent IDs or other data.
#'
#' @param value The raid value to be stored.
#' @param id (Optional) The id (name) of the value in the data.
#' If not provided, the id of the `value` variable will be used.
#'
#' @return NULL (invisibly)
#'
#' @examples
#' \dontrun{
#' sd_store_value(respondentID)
#' sd_store_value(respondentID, "respID")
#' }
#'
#' @export
sd_store_value <- function(value, id = NULL) {
if (is.null(id)) {
id <- deparse(substitute(value))
}

shiny::isolate({
session <- shiny::getDefaultReactiveDomain()
if (is.null(session)) {
stop("sd_store_value must be called from within a Shiny reactive context")
}
if (is.null(session$userData$stored_values)) {
session$userData$stored_values <- list()
}
session$userData$stored_values[[id]] <- value
})

invisible(NULL)
}

#' Create a copy of an input value
#'
#' This function creates a copy of an input value and makes it available as a new output.
#' The new output can then be displayed using sd_display_value().
#'
#' @param id The ID of the input value to copy
#' @param id_copy The ID for the new copy (must be different from id)
#'
#' @return NULL invisibly. This function is called for its side effects.
#'
#' @examples
#' \dontrun{
#' sd_copy_value(id = "respondent_name", id_copy = "resp_name2")
#'
#' # Then in UI:
#' # sd_display_value("resp_name2")
#' }
#'
#' @export
sd_copy_value <- function(id, id_copy) {
if (id == id_copy) {
stop("The 'id_copy' must be different from the 'id'")
}
shiny::isolate({
output <- shiny::getDefaultReactiveDomain()$output
input <- shiny::getDefaultReactiveDomain()$input
output_id <- paste0(id_copy, "_value")
if (!is.null(output)) {
output[[output_id]] <- shiny::renderText({ input[[id]] })
} else {
warning("sd_copy_value was not called within a Shiny reactive context")
}
})
invisible(NULL)
}
73 changes: 0 additions & 73 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,79 +304,6 @@ sd_display_value <- function(id, display_type = "inline", wrapper = NULL, ...) {
return(output)
}

#' Store a value
#'
#' This function allows storing additional values to be included in the survey data,
#' such as respondent IDs or other data.
#'
#' @param value The raid value to be stored.
#' @param id (Optional) The id (name) of the value in the data.
#' If not provided, the id of the `value` variable will be used.
#'
#' @return NULL (invisibly)
#'
#' @examples
#' \dontrun{
#' sd_store_value(respondentID)
#' sd_store_value(respondentID, "respID")
#' }
#'
#' @export
sd_store_value <- function(value, id = NULL) {
if (is.null(id)) {
id <- deparse(substitute(value))
}

shiny::isolate({
session <- shiny::getDefaultReactiveDomain()
if (is.null(session)) {
stop("sd_store_value must be called from within a Shiny reactive context")
}
if (is.null(session$userData$stored_values)) {
session$userData$stored_values <- list()
}
session$userData$stored_values[[id]] <- value
})

invisible(NULL)
}

#' Create a copy of an input value
#'
#' This function creates a copy of an input value and makes it available as a new output.
#' The new output can then be displayed using sd_display_value().
#'
#' @param id The ID of the input value to copy
#' @param id_copy The ID for the new copy (must be different from id)
#'
#' @return NULL invisibly. This function is called for its side effects.
#'
#' @examples
#' \dontrun{
#' sd_copy_value(id = "respondent_name", id_copy = "resp_name2")
#'
#' # Then in UI:
#' # sd_display_value("resp_name2")
#' }
#'
#' @export
sd_copy_value <- function(id, id_copy) {
if (id == id_copy) {
stop("The 'id_copy' must be different from the 'id'")
}
shiny::isolate({
output <- shiny::getDefaultReactiveDomain()$output
input <- shiny::getDefaultReactiveDomain()$input
output_id <- paste0(id_copy, "_value")
if (!is.null(output)) {
output[[output_id]] <- shiny::renderText({ input[[id]] })
} else {
warning("sd_copy_value was not called within a Shiny reactive context")
}
})
invisible(NULL)
}

#' Create a 'Next' Button for Page Navigation
#'
#' This function creates a 'Next' button for navigating to the specified next page in a Surveydown survey.
Expand Down

0 comments on commit 74aa206

Please sign in to comment.