Skip to content

Commit

Permalink
Merge pull request #185 from allometric/lfs
Browse files Browse the repository at this point in the history
Implementing model.RDS download
  • Loading branch information
brycefrank committed Dec 28, 2023
2 parents d9ce36c + 8e84f3b commit 38e0ec8
Show file tree
Hide file tree
Showing 110 changed files with 325 additions and 446 deletions.
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
^pkgdown$
^_pkgdown\.yml$
^inst/extdata/allometric_models\.RDS$
^inst/extdata/pub_list\.RDS$
^inst/extdata/params_path\.RDS$
^LICENSE\.md$
^README\.Rmd$
^Meta$
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(check_models_installed)
export(get_component_defs)
export(get_measure_defs)
export(get_variable_def)
export(ingest_models)
export(install_models)
export(load_models)
export(load_parameter_frame)
Expand Down
42 changes: 23 additions & 19 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,45 @@ download_models <- function(verbose) {
#' \href{https://github.com/allometric/models}{here}. The user must install
#' these models themselves using this function. This function clones the models
#' repository within the allometric package directory and constructs a local
#' dataframe containing the models. This dataframe is stored in the global
#' environment variable `allometric_models` upon completion of the function.
#' Refer to `allometric::allometric_models` for further information.
#' dataframe containing the models. Refer to `load_models()` for information
#' about loading the models dataframe.
#'
#' @param ingest If `TRUE`, model publication files are run locally, otherwise
#' a previously prepared `.RDS` file is used as the models data.
#' @param redownload If `TRUE`, models are re-downloaded from the remote
#' repository.
#' @param ignore_cache If `TRUE`, models are re-installed regardless of their
#' installation timestamp. Otherwise, only newly modified model files are reran.
#' This is primarily for development purposes.
#' @param verbose If `TRUE`, print verbose messages as models are installed.
#' @return No return value, installs models into the package directory.
#' @export
install_models <- function(redownload = FALSE,
ignore_cache = FALSE, verbose = TRUE
) {
install_models <- function(ingest = FALSE, redownload = TRUE, verbose = TRUE) {
downloaded <- check_models_downloaded(verbose)

if(!downloaded || redownload) {
if (!downloaded || redownload) {
download_models(verbose)
}

allometric_models <- ingest_models(verbose)
if (ingest) {
models <- ingest_models(verbose)

out_path <- file.path(
system.file("extdata", package = "allometric"), "allometric_models.RDS"
)
out_path <- file.path(
system.file("models-main", package = "allometric"), "models.RDS"
)

if(verbose) {
n_models <- nrow(allometric_models)
saveRDS(models, out_path)
} else {
models <- readRDS(
file.path(
system.file("models-main", package = "allometric"), "models.RDS"
)
)
}

if (verbose) {
n_models <- nrow(models)
msg <- paste(
n_models,
"models succesfully installed, use load_models() to view them.\n"
"models are currently installed, use load_models() to view them.\n"
)
cat(msg)
}

saveRDS(allometric_models, out_path)
}
21 changes: 17 additions & 4 deletions R/load.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
#' @export
load_parameter_frame <- function(name) {
csv_name <- paste(name, ".csv", sep = "")

if(allometric_options$param_search_path == "package") {
param_search_path <- read_params_path()

if(param_search_path == "package") {
file_path <- system.file(
"models-main/parameters", csv_name,
package = "allometric"
)
} else {
file_path <- file.path(allometric_options$param_search_path, csv_name)
file_path <- file.path(param_search_path, csv_name)
}

table <- utils::read.csv(file_path, na.strings = "")
tibble::as_tibble(table)
}

write_params_path_rds <- function(params_path) {
params_path <- list(params_path = params_path)
rds_path <- file.path(system.file("extdata", package = "allometric"), "params_path.RDS")
saveRDS(params_path, rds_path)
}

read_params_path <- function() {
rds_path <- file.path(system.file("extdata", package = "allometric"), "params_path.RDS")
rds <- readRDS(rds_path)
rds$params_path
}

my_function <- function(family, genus, species) {
Taxa(
Taxon(
Expand Down Expand Up @@ -220,7 +233,7 @@ aggregate_taxa <- function(table, remove_taxa_cols = TRUE) {
#' @export
load_models <- function() {
rds_path <- system.file(
"extdata/allometric_models.RDS",
"models-main/models.RDS",
package = "allometric"
)

Expand Down
18 changes: 15 additions & 3 deletions R/publication_processing.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ map_publications <- function(verbose, func, pub_path = NULL, params_path = NULL)
}

if(!is.null(params_path)) {
allometric_options[["param_search_path"]] <- params_path
write_params_path_rds(params_path)
}

pub_specs <- get_pub_file_specs(pub_path)
Expand All @@ -158,7 +158,14 @@ map_publications <- function(verbose, func, pub_path = NULL, params_path = NULL)
pub <- get(pub_name, envir = pub_env)
output[[pub_name]] <- func(pub)
}, error = function(e) {
warning(paste("Publication file", pub_name, "encountered an error during execution."))
warning(
paste(
"Publication file",
pub_name,
"encountered an error during execution:",
e
)
)
})

if (verbose) {
Expand All @@ -171,12 +178,17 @@ map_publications <- function(verbose, func, pub_path = NULL, params_path = NULL)

# reset the param search path
if(!is.null(params_path)) {
allometric_options[["param_search_path"]] <- "package"
write_params_path_rds("pacakge")
}

output
}

#' Ingest a set of models by running the publication files
#'
#' @param pub_path A path to a directory containing publication files
#' @param params_path A path to a directory containing parameter files
#' @export
ingest_models <- function(verbose, pub_path = NULL, params_path = NULL) {
out_order <- c(
"id", "model_type", "country", "region", "taxa"
Expand Down
4 changes: 1 addition & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ suffixes <- utils::read.csv(

var_defs <- prepare_var_defs(var_defs_pre, measure_defs, component_defs)

allometric_options <- list(
param_search_path = "package"
)
write_params_path_rds("package")
5 changes: 5 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ url: http://allometric.github.io/allometric
template:
params:
bootswatch: cosmo
navbar:
structure:
left: [intro, reference, articles]
right: [search, github]
reference:
- title: Analysis Functions
desc: These functions are used to conduct inventory analysis with allometric models and inspect their contents.
Expand Down Expand Up @@ -37,6 +41,7 @@ reference:
- get_variable_def
- get_component_defs
- get_measure_defs
- ingest_models
- load_parameter_frame
- map_publications
- FixedEffectsModel
Expand Down
3 changes: 0 additions & 3 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/articles/descriptors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/articles/installing_a_model.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions docs/articles/inventory_example.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions docs/articles/variable_naming_system.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/articles/version_two.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 38e0ec8

Please sign in to comment.