Skip to content

Commit

Permalink
Merge pull request #152 from surveydown-dev/dependencies
Browse files Browse the repository at this point in the history
v0.7.0, restructuring of dependency handling
  • Loading branch information
jhelvy authored Dec 6, 2024
2 parents 6127088 + c4c5af4 commit bc53a3b
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 151 deletions.
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' and 'shiny'
Version: 0.6.2
Version: 0.7.0
Authors@R: c(
person(given = "John Paul",
family = "Helveston",
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# surveydown (development version)

# surveydowb 0.6.2
# surveydown 0.7.0

- Heavy revision to how css and us dependencies are loaded. Now the `sd_ui()` function handles the rendering of the `survey.qmd` file and extracting the header contents. All surveydown css and js dependencies are loaded via a simple lua filter when rendering. This simplifies how these dependencies get loaded into the resulting shiny app.

# surveydown 0.6.2

- Update: Now `ignore = TRUE` in `sd_server()` will turn off cookies, regardless of the value of `use_cookies`.

Expand Down
66 changes: 11 additions & 55 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,38 @@ run_config <- function(
# Get paths to files and create '_survey' folder if necessary
paths <- get_paths()

# Check for changes in survey.qmd and app.R files
files_need_updating <- check_files_need_updating(paths)

if (files_need_updating) {
message("Changes detected. Rendering contents.")
# If changes detected, re-parse the '_survey/survey.html' file
if (survey_files_need_updating(paths)) {
message("Changes detected...re-parsing survey contents...")

# Prepare translations (check for inputs)
set_translations(paths, language)

# Render the qmd file into the "_survey" folder
render_qmd(paths)

# Get the html content from the rendered survey.html file
html_content <- rvest::read_html(paths$target_html)

# Extract head content (for CSS and JS) and save to "_survey" folder
head_content <- extract_head_content(paths, html_content)

# Extract all divs with class "sd-page" and save to "_survey" folder
pages <- extract_html_pages(
paths, html_content, required_questions,
all_questions_required, show_if
)

# Get the question structure (If changes detected, extract from HTML, otherwise YAML)
# Get question structure
question_structure <- get_question_structure(paths, html_content)

message(
"Survey saved to:\n",
" ", paths$target_html, "\n",
"Contents saved to:\n",
"Survey contents saved to:\n",
" ", paths$target_pages, "\n",
" ", paths$target_head, "\n",
" ", paths$target_questions
)

} else {
# If no changes, import from '_survey' folder
message(
'No changes detected. Importing contents from "_survey" folder.'
)

# Load head content from _survey folder
head_content <- readRDS(paths$target_head)

# Load pages object from _survey folder
pages <- readRDS(paths$target_pages)

Expand Down Expand Up @@ -97,7 +85,6 @@ run_config <- function(
# Store all config settings
config <- list(
pages = pages,
head_content = head_content,
page_ids = page_ids,
question_ids = question_ids,
question_required = question_required,
Expand Down Expand Up @@ -137,22 +124,19 @@ get_paths <- function() {
return(paths)
}

check_files_need_updating <- function(paths) {
# Re-render if any of the target files are missing
targets <- c(
paths$target_html, paths$target_pages,
paths$target_head, paths$target_questions
)
survey_files_need_updating <- function(paths) {
# Re-parse if any of the target files are missing
targets <- c(paths$target_pages, paths$target_questions)
if (any(!fs::file_exists(targets))) { return(TRUE) }

# Re-render if the target pages file is out of date with 'survey.qmd', 'app.R'
# Re-parse if the target pages file is out of date with 'survey.qmd', 'app.R'
time_qmd <- file.info(paths$qmd)$mtime
time_app <- file.info(paths$app)$mtime
time_pages <- file.info(paths$target_pages)$mtime

if ((time_qmd > time_pages) || (time_app > time_pages)) { return(TRUE) }

# Re-render if the user provided a 'translations.yml' file which is out of date
# Re-parse if the user provided a 'translations.yml' file which is out of date
if (fs::file_exists(paths$transl)) {
time_transl <- file.info(paths$transl)$mtime
if (time_transl > time_pages) { return(TRUE) }
Expand Down Expand Up @@ -231,34 +215,6 @@ set_translations <- function(paths, language) {
yaml::write_yaml(translations, paths$target_transl)
}

render_qmd <- function(paths) {
tryCatch(
{
# Render the 'survey.qmd' file
quarto::quarto_render(
paths$qmd,
pandoc_args = c("--embed-resources")
)

# Move rendered 'survey.html' into '_survey' folder
fs::file_move(paths$root_html, paths$target_html)
},
error = function(e) {
stop("Error rendering 'survey.qmd' file. Please review and revise the file. Error details: ", e$message)
}
)
}

extract_head_content <- function(paths, html_content) {
head_content <- html_content |>
rvest::html_element("head") |>
rvest::html_children() |>
sapply(as.character) |>
paste(collapse = "\n")
saveRDS(head_content, paths$target_head)
return(head_content)
}

extract_html_pages <- function(
paths, html_content, required_questions, all_questions_required, show_if
) {
Expand Down
2 changes: 1 addition & 1 deletion R/db.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ sd_database <- function(
) {

if (ignore) {
message("Database connection ignored. Saving data to local CSV file.")
message("Database connection ignored. Saving data to local CSV file.\n")
return(NULL)
}

Expand Down
20 changes: 9 additions & 11 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@
#'
#' @export
sd_server <- function(
db = NULL,
required_questions = NULL,
all_questions_required = FALSE,
start_page = NULL,
admin_page = FALSE,
auto_scroll = FALSE,
rate_survey = FALSE,
language = "en",
use_cookies = TRUE
db = NULL,
required_questions = NULL,
all_questions_required = FALSE,
start_page = NULL,
admin_page = FALSE,
auto_scroll = FALSE,
rate_survey = FALSE,
language = "en",
use_cookies = TRUE
) {

# 1. Initialize local variables ----
Expand Down Expand Up @@ -154,7 +154,6 @@ sd_server <- function(

# Create local objects from config file
pages <- config$pages
head_content <- config$head_content
page_ids <- config$page_ids
question_ids <- config$question_ids
question_structure <- config$question_structure
Expand Down Expand Up @@ -489,7 +488,6 @@ sd_server <- function(
output$main <- shiny::renderUI({
current_page <- get_current_page()
shiny::tagList(
shiny::tags$head(shiny::HTML(head_content)),
shiny::tags$div(
class = "content",
shiny::tags$div(
Expand Down
Loading

0 comments on commit bc53a3b

Please sign in to comment.