Skip to content

Commit

Permalink
Merge pull request #215 from UI-Research/iss214_fix
Browse files Browse the repository at this point in the history
Iss214 fix
  • Loading branch information
awunderground authored Jun 26, 2023
2 parents 9083bde + 6b1e54b commit b07f7c4
Show file tree
Hide file tree
Showing 32 changed files with 13,071 additions and 609 deletions.
58 changes: 42 additions & 16 deletions R/prep_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ library(glue)
#' @param state_title (boolean) Default to FALSE. The function returns a dataframe
#' with one column being a nested list (see below). One component of the list
#' is an element named "state_title." This argument is written into that element
#' @param geography "county" or "place"
#' @returns run (tibble) A dataframe with three columns:
#' filename - set to be "index.html"
#' params - contains a list of the fips code listed in the fips and comparisons
#' columns of the datasets pointed to by the url argument
#' dirname - location of where knitted hmtl files should be saved
#'
prep_pages <- function(url, output_directory, state_title = FALSE, fake_labels = FALSE, bespoke = TRUE) {
prep_pages <- function(url, output_directory, state_title = FALSE,
fake_labels = FALSE, bespoke = TRUE, geography = "county") {

# read in the applicant list
app_list <- read_csv(url)
Expand All @@ -53,24 +55,48 @@ prep_pages <- function(url, output_directory, state_title = FALSE, fake_labels =
app_list$uni_id
)

if ("random_id" %in% names(app_list)) {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_county = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = paste0(output_directory, '/', full_name_lst, "_", app_list$random_id, "/")
) # creates a nest list of parameters for each object in the index
if (geography == "county") {

} else {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_county = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = map(full_name_lst, function(x) paste0(output_directory, glue('/{x}/')))
) # creates a nest list of parameters for each object in the index
if ("random_id" %in% names(app_list)) {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_county = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = paste0(output_directory, '/', full_name_lst, "_", app_list$random_id, "/")
) # creates a nest list of parameters for each object in the index

} else {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_county = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = map(full_name_lst, function(x) paste0(output_directory, glue('/{x}/')))
) # creates a nest list of parameters for each object in the index

}

} else if (geography == "place") {

if ("random_id" %in% names(app_list)) {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_place = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = paste0(output_directory, '/', full_name_lst, "_", app_list$random_id, "/")
) # creates a nest list of parameters for each object in the index

} else {
# create a data frame with parameters and output file names
runs <- tibble(
filename = "index.html", # creates a string with output file names in the form <index>.pdf
params = map(app_indexes, ~list(state_place = ., state_title = state_title, fake_labels = fake_labels)),
dir_name = map(full_name_lst, function(x) paste0(output_directory, glue('/{x}/')))
) # creates a nest list of parameters for each object in the index

}

}

return(runs)

}
104 changes: 41 additions & 63 deletions R/quarto_render_wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,88 +15,66 @@
#' output_file path

quarto_render_wrapper <- function(input, output_file, execute_params, dir_name) {

# delete directory and contents if it exists
# delete the directory if it exists
if (dir.exists(dir_name)) {

unlink(dir_name, recursive = TRUE)

}

# create directory
# create destination directory for data tables
if (!dir.exists(dir_name)){

dir.create(dir_name)

}

# copy index.qmd
template_name <- paste0(dir_name, "index.qmd")
if (!file.exists(template_name)) {

file.copy(
from = input,
to = template_name
)

}
# copy supporting files to destination directory
www_name <- paste0(dir_name, "www")
dir.create(www_name)
file.copy(
from = "www",
to = dir_name,
recursive = TRUE
)

# copy project yaml
yaml_name <- paste0(dir_name, "_quarto.yml")
if (!file.exists(yaml_name)) {

file.copy(
from = "_quarto.yml",
to = yaml_name
)

}
# copy description html to destination directory
description_html_name <- paste0(dir_name, "description.html")
file.copy(
from = "description.html",
to = description_html_name,
overwrite = TRUE
)

# copy description .qmd
description_qmd_name <- paste0(dir_name, "description.qmd")
if (!file.exists(description_qmd_name)) {

file.copy(
from = "description.qmd",
to = description_qmd_name
)

}
# copy input to generic index.qmd
# this is needed so _quarto.yml will work with the county and city templates
file.copy(
from = input,
to = paste0(dir_name, "index.qmd")
)

# copy description html
description_html_name <- paste0(dir_name, "description.html")
if (!file.exists(description_html_name)) {

file.copy(
from = "description.html",
to = description_html_name
)
# this file ensures that the rendered document is a full website instead of
# individual pages
file.copy(
from = "_quarto.yml",
to = paste0(dir_name, "_quarto.yml")
)

}

www_name <- paste0(dir_name, "www")
if (!file.exists(www_name)) {

dir.create(www_name)

file.copy(
from = "www",
to = dir_name,
recursive = TRUE
# render the quarto document in the new directory
# rendering in the top level directory and then copying does not work
xfun::in_dir(
dir = dir_name,
expr = quarto_render(
input = "index.qmd",
output_file = basename(output_file),
execute_params = execute_params
)

}


quarto_render(
input = template_name,
output_file = output_file,
execute_params = execute_params
)

file.remove(template_name)
file.remove(description_qmd_name)
file.remove(yaml_name)
# delete extra files
file.remove(paste0(dir_name, "search.json"))
file.remove(paste0(dir_name, "index.qmd"))

}

Expand Down
8 changes: 8 additions & 0 deletions create_bespoke_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,11 @@ prepped25 <- prep_pages(url = "data/25_upward-county.csv",
fake_labels = "yes")

render_pages(prepped_object = prepped25, input = "index-county.qmd")

# Upward City, MB
prepped26 <- prep_pages(url = "data/26_upward-city.csv",
output_directory = "factsheets/26_upward-city",
fake_labels = "yes",
geography = "place")

render_pages(prepped_object = prepped26, input = "index-place.qmd")
4 changes: 2 additions & 2 deletions data/25_upward-county.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
state,county,fips,comparisons,random_id
MB, Upward County,51760,,
state,county,fips,comparisons
MB, Upward County,51760,
2 changes: 2 additions & 0 deletions data/26_upward-city.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
state,county,fips,comparisons
MB, Upward County,51760,
Loading

0 comments on commit b07f7c4

Please sign in to comment.