|
11 | 11 | #' |
12 | 12 | #' @examples |
13 | 13 | #' local_location <- tempdir() |
14 | | -#' copy_models(pkgname = "staninside", local_location = local_location) |
| 14 | +#' copy_models(pkgname = "staninside", local_location = local_location) |
| 15 | +copy_models <- function(pkgname = "staninside", local_location = NULL) { |
| 16 | + if (is.null(local_location)) { |
| 17 | + local_location <- rappdirs::user_cache_dir(appname = pkgname) |
| 18 | + cli::cli_alert("`{local_location}` will be used") |
| 19 | + } |
15 | 20 |
|
16 | | -copy_models <- function(pkgname = "staninside", local_location = NULL){ |
| 21 | + stan_code_to_copy <- find_stan_code(pkgname) |
17 | 22 |
|
18 | | - if(is.null(local_location)){ |
19 | | - local_location <- rappdirs::user_cache_dir(appname = pkgname) |
20 | | - cli::cli_alert("`{local_location}` will be used") |
21 | | - } |
| 23 | + pkg_location <- names(stan_code_to_copy$base_stan_location) |
22 | 24 |
|
23 | | - stan_code_to_copy <- find_stan_code(pkgname) |
| 25 | + new_locations_base <- gsub(pattern = ".+/stan/", "", pkg_location) |
24 | 26 |
|
25 | | - pkg_location <- names(stan_code_to_copy$base_stan_location) |
| 27 | + new_locations <- file.path(local_location, new_locations_base) |
26 | 28 |
|
27 | | - new_locations_base <- gsub(pattern = ".+/stan/","",pkg_location) |
| 29 | + dirs_needed <- unique(dirname(new_locations_base)) |
28 | 30 |
|
29 | | - new_locations <- file.path(local_location,new_locations_base) |
| 31 | + dirs_needed <- dirs_needed[dirs_needed != "."] |
30 | 32 |
|
31 | | - dirs_needed <- unique(dirname(new_locations_base)) |
| 33 | + dirs_needed <- dirs_needed[!grepl(dirs_needed, pattern = ".stan")] |
32 | 34 |
|
33 | | - dirs_needed <- dirs_needed[dirs_needed!="."] |
34 | 35 |
|
35 | | - dirs_needed <- dirs_needed[!grepl(dirs_needed, pattern = ".stan")] |
| 36 | + dirs_needed <- dirs_needed[!vapply(file.path(local_location, dirs_needed), |
| 37 | + function(x) fs::dir_exists(path = x), |
| 38 | + FUN.VALUE = logical(1) |
| 39 | + )] |
36 | 40 |
|
| 41 | + if (!fs::dir_exists(local_location)) { |
| 42 | + fs::dir_create(local_location) |
| 43 | + } |
37 | 44 |
|
38 | | - dirs_needed <- dirs_needed[!vapply(file.path(local_location,dirs_needed), |
39 | | - function(x) fs::dir_exists(path = x), FUN.VALUE = logical(1))] |
40 | 45 |
|
41 | | - if(!fs::dir_exists(local_location)){ |
42 | | - fs::dir_create(local_location) |
43 | | - } |
| 46 | + if (length(dirs_needed) > 0) { |
| 47 | + lapply( |
| 48 | + file.path(local_location, dirs_needed), |
| 49 | + function(x) fs::dir_create(path = x, recurse = TRUE) |
| 50 | + ) |
| 51 | + } |
44 | 52 |
|
| 53 | + fs::file_copy( |
| 54 | + path = pkg_location, |
| 55 | + new_path = new_locations, |
| 56 | + overwrite = TRUE |
| 57 | + ) |
45 | 58 |
|
46 | | - if(length(dirs_needed)>0){ |
47 | | - lapply(file.path(local_location,dirs_needed), |
48 | | - function(x) fs::dir_create(path = x, recurse = TRUE)) |
49 | | - } |
| 59 | + cli::cli_alert_success("All models have been copied to your specified location") |
50 | 60 |
|
51 | | - fs::file_copy(path = pkg_location, |
52 | | - new_path =new_locations, |
53 | | - overwrite = TRUE) |
54 | | - |
55 | | - cli::cli_alert_success("All models have been copied to your specified location") |
56 | | - |
57 | | - invisible(new_locations) |
| 61 | + invisible(new_locations) |
58 | 62 | } |
0 commit comments