|
| 1 | +# WARNING - Generated by {fusen} from dev/flat_save_att_params.Rmd: do not edit by hand |
| 2 | + |
| 3 | +#' load_att_params |
| 4 | +#' |
| 5 | +#' @param path_to_yaml character The path to the yaml file |
| 6 | +#' |
| 7 | +#' @importFrom yaml read_yaml |
| 8 | +#' @importFrom glue glue |
| 9 | +#' |
| 10 | +#' @return list A named list of att_amend_desc parameters |
| 11 | +#' |
| 12 | +#' @noRd |
| 13 | +#' @examples |
| 14 | +#' # create a list of parameters and tmp file name |
| 15 | +#' parameter_list <- list( |
| 16 | +#' pkg_ignore = c("remotes", "i"), |
| 17 | +#' extra.suggests = c("testthat", "rstudioapi") |
| 18 | +#' ) |
| 19 | +#' yaml_path <- paste0(tempfile(pattern = "save_att"), ".yaml") |
| 20 | +#' |
| 21 | +#' # save params |
| 22 | +#' save_att_params(param_list = parameter_list, |
| 23 | +#' path_to_yaml = yaml_path) |
| 24 | +#' |
| 25 | +#' # read yaml file |
| 26 | +#' config_params <- load_att_params(path_to_yaml = yaml_path) |
| 27 | +#' |
| 28 | +#' # clear created yaml file |
| 29 | +#' unlink(yaml_path) |
| 30 | +#' |
| 31 | +load_att_params <- function( |
| 32 | + path_to_yaml = "dev/config_attachment.yaml", |
| 33 | + verbose = FALSE |
| 34 | + ){ |
| 35 | + |
| 36 | + # check yaml file exist |
| 37 | + if (isFALSE(file.exists(path_to_yaml))){ |
| 38 | + stop(glue("The att_amend_desc() config file {path_to_yaml} does not exist")) |
| 39 | + } |
| 40 | + |
| 41 | + # read yaml |
| 42 | + param_list <- read_yaml(file = path_to_yaml) |
| 43 | + |
| 44 | + # Check each name in list corresponds to a parameter name |
| 45 | + att_param_names <- names(formals(att_amend_desc)) |
| 46 | + input_names <- names(param_list) |
| 47 | + all_inputs_are_params <- all(input_names %in% att_param_names) |
| 48 | + if (isFALSE(all_inputs_are_params)){ |
| 49 | + bad_names <- input_names[!input_names %in% att_param_names] |
| 50 | + stop(paste0("Unexpected parameters in config : ", paste0(bad_names, collapse = " ; "))) |
| 51 | + } |
| 52 | + |
| 53 | + # Show parameters used from config |
| 54 | + if (isTRUE(verbose)) { |
| 55 | + message("att_amend_desc() parameter loaded are : \n", |
| 56 | + paste0( |
| 57 | + glue("{names(param_list)} = {param_list}"), |
| 58 | + collapse = "\n" |
| 59 | + ) |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + # return parameters |
| 64 | + return(param_list) |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +#' save_att_params |
| 69 | +#' |
| 70 | +#' @param param_list list A named list of all parameters to save |
| 71 | +#' @param path_to_yaml character The path to the yaml file |
| 72 | +#' @param overwrite logical Whether to overwrite the yaml file if it already exists |
| 73 | +#' |
| 74 | +#' @importFrom yaml write_yaml |
| 75 | +#' |
| 76 | +#' @return character The path to the yaml file |
| 77 | +#' @noRd |
| 78 | +#' @examples |
| 79 | +#' # create a list of parameters and tmp file name |
| 80 | +#' parameter_list <- list( |
| 81 | +#' pkg_ignore = c("remotes", "i"), |
| 82 | +#' extra.suggests = c("testthat", "rstudioapi") |
| 83 | +#' ) |
| 84 | +#' yaml_path <- paste0(tempfile(pattern = "save_att"), ".yaml") |
| 85 | +#' |
| 86 | +#' # save params |
| 87 | +#' save_att_params(param_list = parameter_list, |
| 88 | +#' path_to_yaml = yaml_path) |
| 89 | +#' |
| 90 | +#' yaml::read_yaml(yaml_path) |
| 91 | +#' # rstudioapi::navigateToFile(yaml_path) |
| 92 | +#' |
| 93 | +#' # clear created yaml file |
| 94 | +#' unlink(yaml_path) |
| 95 | +save_att_params <- function( |
| 96 | + param_list, |
| 97 | + path_to_yaml = "dev/config_attachment.yaml", |
| 98 | + overwrite = FALSE |
| 99 | + ) { |
| 100 | + |
| 101 | + # Check each name in list corresponds to a parameter name |
| 102 | + att_param_names <- names(formals(att_amend_desc)) |
| 103 | + input_names <- names(param_list) |
| 104 | + all_inputs_are_params <- all(input_names %in% att_param_names) |
| 105 | + if (isFALSE(all_inputs_are_params)){ |
| 106 | + bad_names <- input_names[!input_names %in% att_param_names] |
| 107 | + stop(paste0("Unexpected parameters to save : ", paste0(bad_names, collapse = " ; "))) |
| 108 | + } |
| 109 | + |
| 110 | + # Create dir if missing |
| 111 | + dir_yaml <- normalizePath(dirname(path_to_yaml), mustWork = FALSE) |
| 112 | + if (!dir.exists(dir_yaml)) {dir.create(dir_yaml)} |
| 113 | + |
| 114 | + # Write params to yaml |
| 115 | + yaml_exists <- file.exists(path_to_yaml) |
| 116 | + |
| 117 | + if (isTRUE(yaml_exists & !overwrite)) { |
| 118 | + stop("yaml file already exists and overwriting is not permitted") |
| 119 | + } else { |
| 120 | + write_yaml( |
| 121 | + x = param_list, |
| 122 | + file = path_to_yaml, |
| 123 | + indent.mapping.sequence = TRUE |
| 124 | + ) |
| 125 | + message("Saving attachment parameters to yaml config file") |
| 126 | + } |
| 127 | + |
| 128 | + return(path_to_yaml) |
| 129 | +} |
0 commit comments