forked from nstrayer/datadrivencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nstrayer#22 from nstrayer/more_flexible_templating
More flexible templating + Tests
- Loading branch information
Showing
24 changed files
with
964 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' Use template file from package | ||
#' | ||
#' @param file_name Name of file from templates to use: e.g. `cv.rmd`. | ||
#' @param params Parameters used to fill in `whisker` template | ||
#' @param output_file_name Name of file after being placed. | ||
#' @param output_dir Directory location for output to be placed in. | ||
#' @param create_output_dir If the requested output directory is missing should it be created? | ||
#' @param warn_about_no_change If there is no change between the new file and what was already there, should a warning be issued? | ||
#' @param open_after_making Should the file be opened after it has been written? | ||
#' | ||
#' @return NULL | ||
use_ddcv_template <- function( | ||
file_name, | ||
params = NULL, | ||
output_file_name = file_name, | ||
output_dir = getwd(), | ||
create_output_dir = FALSE, | ||
warn_about_no_change = TRUE, | ||
open_after_making = FALSE){ | ||
output_dir_missing <- !fs::dir_exists(output_dir) | ||
|
||
if(output_dir_missing & create_output_dir){ | ||
fs::dir_create(output_dir) | ||
} else | ||
if(output_dir_missing & !create_output_dir) { | ||
stop(glue::glue("The requested output directory: {output_dir} doesn't exist. Either set create_output_dir = TRUE or manually make directory.")) | ||
} | ||
|
||
|
||
template_loc <- fs::path(system.file("templates/", package = "datadrivencv"), file_name) | ||
output_loc <- fs::path(output_dir, output_file_name) | ||
|
||
template_text <- readr::read_file(template_loc) | ||
|
||
if(!is.null(params)){ | ||
template_text <- whisker::whisker.render(template_text, data = params) | ||
} | ||
|
||
# Check if file exists already | ||
already_exists <- fs::file_exists(output_loc) | ||
if(already_exists){ | ||
# Check if the two files are identical | ||
no_changes_made <- readr::read_file(output_loc) == template_text | ||
|
||
if(no_changes_made & warn_about_no_change){ | ||
warning(glue::glue("{file_name} already exists and there are no differences with the current version.")) | ||
} | ||
} | ||
|
||
readr::write_file(template_text, output_loc) | ||
|
||
# Open the file if requested | ||
if(open_after_making){ | ||
if (rstudioapi::isAvailable() && rstudioapi::hasFun("navigateToFile")) { | ||
rstudioapi::navigateToFile(output_loc) | ||
} else { | ||
utils::file.edit(output_loc) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.