Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions R/backtransform.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ backtransform_id <- function(unique_id, group_names) {

#' Backtransform
#'
#' Undoes as.trackframe, returning the dataframe to its previous class.
#' Also attempts to undo sorting performed by as.trackframe.
#'
#' @param tf an object of class `trackframe`
#' @param all undo all as.trackframe calls, not just the most recent
#'
#' @return an object which has been coerced to `trackframe`
#' @export
Expand All @@ -43,7 +47,7 @@ backtransform_id <- function(unique_id, group_names) {
#' tfb <- tf_backtransform(tf)
#' tfb
#' df_mini
tf_backtransform <- function(tf) {
tf_backtransform <- function(tf, all = FALSE) {
assert_class(tf, "trackframe")
transformation_info <- attr(tf, "transformation_info")
if (is.null(transformation_info)) {
Expand All @@ -63,9 +67,6 @@ tf_backtransform <- function(tf) {
attr(tf_bt, "class") <- class_old
attributes(tf_bt)[!names(attributes(tf_bt)) %in% names(transformation_info)] <- NULL
} else if (class_old[1] %in% c("trackframe", "data.frame", "data.table", "tbl_df", "tbl")) {
if (isTRUE(sort)) {
tf <- sort(tf)
}
if (isTRUE(attr(tf, "easting") != transformation_info$coord_names[1])) {
tf[, attr(tf, "easting")] <- NULL
}
Expand Down Expand Up @@ -150,6 +151,9 @@ tf_backtransform <- function(tf) {
if (reorder) {
tf_bt <- tf_bt[match(orig_hash, new_hash), ]
}
if (all && !is.null(transformation_info$attributes$transformation_info)) {
tf_bt <- tf_backtransform(tf_bt, all = TRUE)
}
return(tf_bt)
}

Expand Down
7 changes: 5 additions & 2 deletions R/trackframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,16 @@ as.trackframe.data.frame <- function(
log_debug("- %i set as crs", attr(data, "crs"))
log_debug("- %i set as crs_type", attr(data, "crs_type"))

if (is.null(attr(data, "transformation_info"))) {
if (is.null(attr(data, "tmp_transformation_info"))) {
transformation_info <- list()
transformation_info$attributes <- attributes_input
transformation_info$class <- attributes_input$class
transformation_info$names <- cn_input
transformation_info$coord_names <- c(easting_col, northing_col)
attr(data, "transformation_info") <- transformation_info
} else {
attr(data, "transformation_info") <- attr(data, "tmp_transformation_info")
attr(data, "tmp_transformation_info") <- NULL
}

# sort data by id and time
Expand Down Expand Up @@ -641,7 +644,7 @@ as.trackframe.sf <- function(
}

attr(data, "row.names") <- data_attr[["row.names"]]
attr(data, "transformation_info") <- transformation_info
attr(data, "tmp_transformation_info") <- transformation_info
as.trackframe(
data,
time_col = time_col,
Expand Down
10 changes: 10 additions & 0 deletions inst/tinytest/test_backtransform.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ tf <- as.trackframe(data = sftrack_a)
sftrack_b <- tf_backtransform(tf)
expect_equal(sftrack_b, sftrack_a)

tf2 <- as.trackframe(tf, coerce_to = "data.table")
expect_equal(tf_backtransform(tf2), tf)

expect_equal(tf_backtransform(tf_backtransform(tf2)), sftrack_a)

expect_equal(
tf_backtransform(tf_backtransform(tf2)),
tf_backtransform(tf2, all = TRUE)
)

# test with multiple ids
data("raccoon", package = "sftrack")
raccoon$month <- as.POSIXlt(raccoon$timestamp)$mon + 1
Expand Down
7 changes: 5 additions & 2 deletions man/tf_backtransform.Rd

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

Loading