Skip to content

Commit

Permalink
Merge 06ff460 into 35d6522
Browse files Browse the repository at this point in the history
  • Loading branch information
brews committed Jul 24, 2019
2 parents 35d6522 + 06ff460 commit d23a639
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ S3method(print,sea)
S3method(quantile,intervals)
S3method(sort,fhx)
S3method(summary,fhx)
export(as.fhx)
export(composite)
export(count_event_position)
export(count_injury)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Changes in this release:

* Added `as.fhx()`. This takes data frames, tibbles, and lists as input. It assumes they have "year", "series", and "rec_type" elements/columns and returns an fhx object. (Issue #120).

* Removed deprecated `run_sea()`. Be sure to use `sea()` now.

* Removed deprecated `get_ggplot()`, please use `plot_demograph()` now.
Expand Down
27 changes: 27 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,33 @@ is.fhx <- function(x) {
inherits(x, 'fhx')
}

#' Convert to fhx object.
#'
#' @param x A data frame or list-like object. Must have named elements or columns for "year", "series", and "rec_type".
#'
#' @return Boolean indicating whether `x` is an fhx object.
#'
#' @details
#' The "year", "series", and "rec_type" in \code{x} will be pass through \code{as.numeric}, \code{as.factor}, and \code{burnr::make_rec_type} before being passed to \code{burnr::fhx}.
#'
#' @examples
#' data(lgr2)
#' example_dataframe <- as.data.frame(lgr2)
#' back_to_fhx <- as.fhx(example_dataframe)
#'
#' @export
as.fhx <- function(x) {
if (!all(c("year", "series", "rec_type") %in% names(x))) {
stop("`x` must have members 'year', 'series', and 'rec_type'")
}

yr <- as.numeric(x$year)
series <- as.factor(x$series)
record <- make_rec_type(x$rec_type)

fhx(yr, series, record)
}

#' Check for duplicate observations in an fhx object.
#'
#' @param x An fhx object.
Expand Down
26 changes: 26 additions & 0 deletions man/as.fhx.Rd

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

42 changes: 42 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,46 @@ test_that("check_duplicates throws error when fhx obj has duplicates", {
"*Please resolve duplicate records*")
})

test_that("as.fhx works on data.frame input", {
yrs <- c(1850, 2010)
series_chr <- c("a", "a")
events_chr <- c("pith_year", "bark_year")

test_df <- data.frame(year = yrs,
series = series_chr,
rec_type = events_chr)

new_fhx <- burnr::as.fhx(test_df)
expect_equal(new_fhx$year, yrs)
expect_equal(new_fhx$series, factor(series_chr))
expect_equal(new_fhx$rec_type, burnr::make_rec_type(events_chr))
})

test_that("as.fhx works on list input", {
yrs <- c(1850, 2010)
series_chr <- c("a", "a")
events_chr <- c("pith_year", "bark_year")

test_df <- list(year = yrs,
series = series_chr,
rec_type = events_chr)

new_fhx <- burnr::as.fhx(test_df)
expect_equal(new_fhx$year, yrs)
expect_equal(new_fhx$series, factor(series_chr))
expect_equal(new_fhx$rec_type, burnr::make_rec_type(events_chr))
})

test_that("as.fhx throws error when input missing key element", {
yrs <- c(1850, 2010)
series_chr <- c("a", "a")
events_chr <- c("pith_year", "bark_year")

test_df <- list(year = yrs,
serie = series_chr, # bad spelling
rec_type = events_chr)

expect_error(burnr:::as.fhx(test_df),
"`x` must have members 'year', 'series', and 'rec_type'")
})

0 comments on commit d23a639

Please sign in to comment.