R/utils.R

Defines functions get_pkg_name get_pkg_path max_n_taxa min_n_taxa min_mbness to_string2 get_full_filename

Documented in get_full_filename get_pkg_name get_pkg_path max_n_taxa min_mbness min_n_taxa to_string2

#' Get the name for this package
#' @export
get_pkg_name <- function() {
  pkg_name <- "mbd.SimTrees"
  pkg_name
}

#' Get the path for this package
#' @export
get_pkg_path <- function() {
  list.files(getwd())
}

#' Maximum accepted number of taxa
#' @export
max_n_taxa <- function() {
  max_n <- 400
  max_n
}

#' Miminum accepted number of taxa
#' @export
min_n_taxa <- function() {
  min_n <- 5
  min_n
}

#' Minimum accepted amount of mbness
#' @export
min_mbness <- function() {
  min_mbness <- 0.5
  min_mbness
}

#' Transform to string removing blank spaces
#' @inheritParams default_params_doc
#' @export
to_string2 <- function(
  var
) {
  gsub(x = toString(var), pattern = " ", replacement = "")
}

#' get the full filename
#' @inheritParams default_params_doc
#' @export
get_full_filename <- function(
  lambdas = c(0.2),
  mus = c(0, 0.1),
  nus = c(0, 0.5, 1.0, 1.5),
  qs = c(0.1, 0.15, 0.2),
  cond = 1,
  crown_age = 8
) {
  # folder structure
  x <- unlist(strsplit(getwd(), .Platform$file.sep))
  if (get_pkg_name() %in% x) {
    y <- which(x == get_pkg_name())
    project_folder <- paste0(x[1:y], collapse = .Platform$file.sep)
  } else {
    home_folder <- paste0(x, collapse = .Platform$file.sep)
    project_folder <- file.path(home_folder, get_pkg_name())
  }
  rm(x)
  if (!dir.exists(project_folder)) {
    dir.create(project_folder)
  }

  data_folder <- file.path(project_folder, "inst", "extdata")
  if (!dir.exists(data_folder)) {
    dir.create(data_folder)
  }

  parsetting <- paste0(
    "la=", to_string2(lambdas),
    "-",
    "mu=", to_string2(mus),
    "-",
    "nu=", to_string2(nus),
    "-",
    "q=", to_string2(qs),
    "-",
    "age=", to_string2(crown_age)
  )
  parsetting <- gsub(parsetting, pattern = " ", replacement = "")
  parsetting_folder <- file.path(data_folder, parsetting)
  if (!dir.exists(parsetting_folder)) {
    dir.create(parsetting_folder)
  }
  filename <- paste0(
    "result",
    ".Rdata"
  )
  full_filename <- file.path(parsetting_folder, filename)
  full_filename
}
Giappo/mbd.SimTrees documentation built on Jan. 23, 2020, 3:40 a.m.