Skip to content

Commit

Permalink
Improve load_data useability
Browse files Browse the repository at this point in the history
  • Loading branch information
gmyenni committed Sep 18, 2024
1 parent 7e15d88 commit 0f6d786
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: portalr
Title: Create Useful Summaries of the Portal Data
Version: 0.4.2
Version: 0.4.3
Authors@R:
c(person(given = c("Glenda", "M."),
family = "Yenni",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,portal_data_list)
export("%>%")
export(abundance)
export(add_seasons)
Expand Down
89 changes: 55 additions & 34 deletions R/load_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
#' @export
#'
load_rodent_data <- function(path = get_default_data_path(),
download_if_missing = TRUE, clean = TRUE, quiet = FALSE)
download_if_missing = TRUE, clean = TRUE, quiet = FALSE, ...)
{
rodent_data <- load_datafile(file.path("Rodents", "Portal_rodent.csv"),
na.strings = "", path, download_if_missing,
quiet = quiet)
quiet = quiet, ...)
species_table <- load_datafile(file.path("Rodents", "Portal_rodent_species.csv"),
na.strings = "", path, download_if_missing)
na.strings = "", path, download_if_missing, ...)
trapping_table <- load_datafile(file.path("Rodents", "Portal_rodent_trapping.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)
newmoons_table <- load_datafile(file.path("Rodents", "moon_dates.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)
plots_table <- load_datafile(file.path("SiteandMethods", "Portal_plots.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)

# reformat species columns
if (!"species" %in% names(species_table))
Expand All @@ -63,9 +63,10 @@ load_rodent_data <- function(path = get_default_data_path(),
by = c("year", "month", "plot"))
trapping_table <- dplyr::filter(trapping_table, .data$qcflag == 1)
}

return(mget(c("rodent_data", "species_table", "trapping_table",
"newmoons_table", "plots_table")))
tables <- mget(c("rodent_data", "species_table", "trapping_table",
"newmoons_table", "plots_table"))
class(tables) <- append(class(tables), "portal_data_list")
return(tables)
}

#' @rdname load_rodent_data
Expand All @@ -87,30 +88,32 @@ load_rodent_data <- function(path = get_default_data_path(),
#'

load_plant_data <- function(path = get_default_data_path(),
download_if_missing = TRUE, quiet = FALSE)
download_if_missing = TRUE, quiet = FALSE, ...)
{
quadrat_data <- load_datafile(file.path("Plants", "Portal_plant_quadrats.csv"),
na.strings = "", path, download_if_missing,
quiet = quiet)
quiet = quiet, ...)
species_table <- load_datafile(file.path("Plants", "Portal_plant_species.csv"),
na.strings = "", path, download_if_missing)
na.strings = "", path, download_if_missing, ...)
census_table <- load_datafile(file.path("Plants", "Portal_plant_censuses.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)
date_table <- load_datafile(file.path("Plants", "Portal_plant_census_dates.csv"),
na.strings = c("", "none", "unknown"), path, download_if_missing)
na.strings = c("", "none", "unknown"), path, download_if_missing, ...)
plots_table <- load_datafile(file.path("SiteandMethods", "Portal_plots.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)
transect_data <- load_datafile(file.path("Plants", "Portal_plant_transects_2015_present.csv"),
na.strings = "", path, download_if_missing)
na.strings = "", path, download_if_missing, ...)
oldtransect_data <- load_datafile(file.path("Plants", "Portal_plant_transects_1989_2009.csv"),
na.strings = "", path, download_if_missing)
na.strings = "", path, download_if_missing, ...)

# reformat species columns
species_table <- reformat_species_table(species_table)

return(mget(c("quadrat_data", "species_table", "census_table",
"date_table", "plots_table",
"transect_data", "oldtransect_data")))
tables <- mget(c("quadrat_data", "species_table", "census_table",
"date_table", "plots_table",
"transect_data", "oldtransect_data"))
class(tables) <- append(class(tables), "portal_data_list")
return(tables)
}

#' @rdname load_rodent_data
Expand All @@ -128,23 +131,25 @@ load_plant_data <- function(path = get_default_data_path(),
#'

load_ant_data <- function(path = get_default_data_path(),
download_if_missing = TRUE, quiet = FALSE)
download_if_missing = TRUE, quiet = FALSE, ...)
{
bait_data <- load_datafile(file.path("Ants", "Portal_ant_bait.csv"),
na.strings = "", path, download_if_missing,
quiet = TRUE)
quiet = TRUE, ...)
colony_data <- load_datafile(file.path("Ants", "Portal_ant_colony.csv"),
na.strings = "", path, download_if_missing)
na.strings = "", path, download_if_missing, ...)
species_table <- load_datafile(file.path("Ants", "Portal_ant_species.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)
plots_table <- load_datafile(file.path("SiteandMethods", "Portal_plots.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)

# reformat species columns
species_table <- reformat_species_table(species_table)

return(mget(c("bait_data", "colony_data",
"species_table", "plots_table")))
tables <- mget(c("bait_data", "colony_data",
"species_table", "plots_table"))
class(tables) <- append(class(tables), "portal_data_list")
return(tables)
}

#' @rdname load_rodent_data
Expand All @@ -159,12 +164,12 @@ load_ant_data <- function(path = get_default_data_path(),
#' @export
load_trapping_data <- function(path = get_default_data_path(),
download_if_missing = TRUE, clean = TRUE,
quiet = FALSE)
quiet = FALSE, ...)
{
trapping_table <- load_datafile(file.path("Rodents", "Portal_rodent_trapping.csv"),
na.strings = "NA", path, download_if_missing, quiet = quiet)
na.strings = "NA", path, download_if_missing, quiet = quiet, ...)
newmoons_table <- load_datafile(file.path("Rodents", "moon_dates.csv"),
na.strings = "NA", path, download_if_missing)
na.strings = "NA", path, download_if_missing, ...)

# remove data still under quality control
if (clean)
Expand All @@ -173,7 +178,9 @@ load_trapping_data <- function(path = get_default_data_path(),
by = "period")
}

return(mget(c("trapping_table", "newmoons_table")))
tables <- mget(c("trapping_table", "newmoons_table"))
class(tables) <- append(class(tables), "portal_data_list")
return(tables)
}

#' @name load_datafile
Expand All @@ -191,9 +198,8 @@ load_trapping_data <- function(path = get_default_data_path(),
#'
#' @export
load_datafile <- function(datafile, na.strings = "", path = get_default_data_path(),
download_if_missing = TRUE, quiet = TRUE)
download_if_missing = TRUE, quiet = TRUE, ...)
{

## define file paths
if (tolower(path) == "repo")
{
Expand All @@ -210,7 +216,7 @@ load_datafile <- function(datafile, na.strings = "", path = get_default_data_pat
{
if (download_if_missing) {
warning("Proceeding to download data into specified path", path, "\n")
tryCatch(download_observations(path),
tryCatch(download_observations(path, ...),
error = function(e) e,
warning = function(w) w)
} else {
Expand Down Expand Up @@ -249,3 +255,18 @@ reformat_species_table <- function(species_table)
}
return(species_table)
}

#' Prints a portal_data_list object
#' @rdname print
#' @param x A portal_data_list object.
#' @export
#'
print.portal_data_list <- function(x, ...) {
x_name <- deparse(substitute(x))
tbls <- names(x)
message("\nPortal data: a list with the following data frames:\n")
print(tbls, ...)
access_msg <- paste0("'", x_name, "$", tbls[1], "'")
message(paste("\nAccess individual data sets with e.g.", access_msg))
invisible(x)
}
5 changes: 4 additions & 1 deletion man/load_datafile.Rd

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

12 changes: 8 additions & 4 deletions man/load_rodent_data.Rd

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

14 changes: 14 additions & 0 deletions man/print.Rd

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

0 comments on commit 0f6d786

Please sign in to comment.