Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull in latest main to jph-docs #73

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: surveydown
Title: Markdown-Based Surveys Using Quarto Shiny Documents
Version: 0.0.6
Version: 0.0.8
Authors@R: c(
person(given = "John Paul",
family = "Helveston",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(sd_database)
export(sd_next)
export(sd_question)
export(sd_server)
export(sd_set_password)
export(sd_setup)
export(sd_update_extension)
export(sd_update_surveydown)
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# surveydown (development version)

# surveydown 0.0.8

- `sd_database()` function added with `pause` argument default to `FALSE`. If `pause = FALSE`, database will be properly connected; if `pause = TRUE`, a local CSV will be generated and survey results will be stored there.
- `sd_config()` function has `preview` removed due to `pause` in `sd_database`.
- For `pause = FALSE` (aka default), warning messages will be shown for missing or incorrect password, and will prompt the usage of `sd_set_password()`.
- Initiation of the `admin_page` argument in `config.R`.

# surveydown 0.0.7

- `sd_set_password()` function to set the supabase password as the survey environment variable. This function takes in a string, which should be your supabase password. We recommend you to only run it in the R Console so that your password does not appear in the `.qmd` file.
- (Continue) Upon running `sd_set_password()`, an `.Renviron` file will be created in your survey project root directory. In this file, `SUPABASE_PASSWORD=your_password` will be created, with `your_password` being whatever your input of `sd_set_password()`. Then, `.Renviron` will be added to your `.gitignore` file to avoid being pushed to GitHub.
- (Continue) If there is already an `.Renviron` file, `SUPABASE_PASSWORD=your_password` will be concatenated to the end. If there is already a definition of `SUPABASE_PASSWORD`, it will be overwritten. If there is no `.gitignore` file, it will be created. If there is already an `.Renviron` in `.gitignore`, it won't be duplicated.
- (Continue) All the above explanation means that you simply run `sd_set_password()` once to define supabase password for your survey project. It takes care of the rest of necessary operations, and you can rerun `sd_set_password()` to change the password, with the previous value safely overwritten.

# surveydown 0.0.6

- In `sd_database`, a `gssencmode` argument is added and set to "prefer" by default. In some cases, local deployment may fail due to network environments such as VPN settings. It can be solved by setting `gssencmode = "disable"` in the survey `qmd` file.
Expand Down
142 changes: 75 additions & 67 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,41 @@
#' @param skip_if_custom A custom function to handle conditions under which certain pages should be skipped. Defaults to NULL.
#' @param show_if A list of conditions under which certain pages should be shown. Defaults to NULL.
#' @param show_if_custom A custom function to handle conditions under which certain pages should be shown. Defaults to NULL.
#' @param preview Logical. Whether the survey is in preview mode. Defaults to FALSE.
#' @param start_page Character string. The ID of the page to start on. Defaults to NULL.
#' @param show_all_pages Logical. Whether to show all pages initially. Defaults to FALSE.
#' @param admin_page Logical. Whether to include an admin page for viewing and downloading survey data. Defaults to FALSE.
#'
#' @details The function retrieves the survey metadata, checks the validity of the conditional
#' display settings, and ensures that the specified start page (if any) exists. It then stores
#' these settings in a configuration list.
#' these settings in a configuration list. If `admin_page` is set to TRUE, an admin page will be
#' included in the survey. This page allows viewing and downloading of survey data upon entering
#' the correct survey password (set using `sd_set_password()`).
#'
#' @return A list containing the configuration settings for the survey, including page and question
#' structures, conditional display settings, and navigation options.
#' structures, conditional display settings, navigation options, and admin page settings.
#'
#' @examples
#' \dontrun{
#' config <- sd_config(
#' skip_if = list(),
#' skip_if = list(),
#' skip_if_custom = NULL,
#' show_if = list(),
#' show_if = list(),
#' show_if_custom = NULL,
#' preview = FALSE,
#' start_page = "page1",
#' show_all_pages = FALSE
#' start_page = "page1",
#' show_all_pages = FALSE,
#' admin_page = TRUE
#' )
#' }
#'
#' @export
sd_config <- function(
skip_if = NULL,
skip_if = NULL,
skip_if_custom = NULL,
show_if = NULL,
show_if = NULL,
show_if_custom = NULL,
preview = FALSE,
start_page = NULL,
show_all_pages = FALSE
start_page = NULL,
show_all_pages = FALSE,
admin_page = FALSE
) {

# Get survey metadata
Expand All @@ -54,82 +56,88 @@ sd_config <- function(
question_required = sapply(question_structure, `[[`, "required")
)

# Check skip_if and show_if inputs
check_skip_show(config, skip_if, skip_if_custom, show_if, show_if_custom)
# Check skip_if and show_if inputs
check_skip_show(config, skip_if, skip_if_custom, show_if, show_if_custom)

# Check that start_page (if used) points to an actual page
if (!is.null(start_page)) {
if (! start_page %in% config$page_ids) {
stop(
"The specified start_page does not exist - check that you have ",
"not mis-spelled the id"
)
# Check that start_page (if used) points to an actual page
if (!is.null(start_page)) {
if (! start_page %in% config$page_ids) {
stop(
"The specified start_page does not exist - check that you have ",
"not mis-spelled the id"
)
}
}
}

if (show_all_pages) {
for (page in config$page_ids) {
shinyjs::show(page)
if (show_all_pages) {
for (page in config$page_ids) {
shinyjs::show(page)
}
}

if (admin_page) {

# Admin page logic here....

}
}

# Store remaining config settings
config$skip_if <- skip_if
config$skip_if_custom <- skip_if_custom
config$show_if <- show_if
config$show_if_custom <- show_if_custom
config$preview <- preview
config$start_page <- start_page
config$show_all_pages <- show_all_pages

return(config)

# Store remaining config settings
config$skip_if <- skip_if
config$skip_if_custom <- skip_if_custom
config$show_if <- show_if
config$show_if_custom <- show_if_custom
config$start_page <- start_page
config$show_all_pages <- show_all_pages
config$admin_page <- admin_page

return(config)
}

## Page structure ----

get_page_structure <- function() {

# Get all page nodes
page_nodes <- get_page_nodes()
page_ids <- page_nodes |> rvest::html_attr("id")
# Get all page nodes
page_nodes <- get_page_nodes()
page_ids <- page_nodes |> rvest::html_attr("id")

# Initialize a list to hold the results
page_structure <- list()
# Initialize a list to hold the results
page_structure <- list()

# Iterate over each page node to get the question_ids
for (i in seq_along(page_nodes)) {
page_id <- page_ids[i]
page_node <- page_nodes[i]
# Iterate over each page node to get the question_ids
for (i in seq_along(page_nodes)) {
page_id <- page_ids[i]
page_node <- page_nodes[i]

# Extract all question IDs within this page
question_ids <- page_node |>
rvest::html_nodes("[data-question-id]") |>
rvest::html_attr("data-question-id")
# Extract all question IDs within this page
question_ids <- page_node |>
rvest::html_nodes("[data-question-id]") |>
rvest::html_attr("data-question-id")

# Store the question IDs for this page
page_structure[[page_id]] <- question_ids
}
# Store the question IDs for this page
page_structure[[page_id]] <- question_ids
}

return(page_structure)
return(page_structure)
}

get_page_nodes <- function() {

# Get the list of .qmd files in the current working directory
qmd_files <- list.files(pattern = "\\.qmd$", full.names = TRUE)
# Get the list of .qmd files in the current working directory
qmd_files <- list.files(pattern = "\\.qmd$", full.names = TRUE)

# Check if there is exactly one .qmd file
if (length(qmd_files) == 1) {
qmd_file_name <- qmd_files[1]
html_file_name <- sub("\\.qmd$", ".html", qmd_file_name)
# Check if there is exactly one .qmd file
if (length(qmd_files) == 1) {
qmd_file_name <- qmd_files[1]
html_file_name <- sub("\\.qmd$", ".html", qmd_file_name)

# Use the derived HTML file name to read the document with rvest
pages <- rvest::read_html(html_file_name) |>
rvest::html_nodes(".sd-page")
return(pages)
}
# Use the derived HTML file name to read the document with rvest
pages <- rvest::read_html(html_file_name) |>
rvest::html_nodes(".sd-page")
return(pages)
}

stop("Error: {surveydown} requires that only one .qmd file in the directory.")
stop("Error: {surveydown} requires that only one .qmd file in the directory.")

}

Expand Down
Loading
Loading