Skip to content

Commit

Permalink
* Add a dedicated tidyr::drop_na() method for sftime objects. (Se…
Browse files Browse the repository at this point in the history
…e the same recent addition for `sf` objects [#1975](r-spatial/sf#1975)).

* Add an entry for parameter y in the documentation for `sftime::tidyverse` methods.
  • Loading branch information
henningte committed Dec 7, 2022
1 parent 2050570 commit e82f4ac
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: Classes and methods for spatial objects that have a registered time
data (vector or raster data cubes) are handled by package 'stars'.
Version: 0.2.0.9000
Depends:
sf (>= 1.0.7)
sf (>= 1.0.9)
Imports: methods
Suggests:
knitr,
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# sftime (development version)

* Add a dedicated `tidyr::drop_na()` method for `sftime` objects. (See the same recent addition for `sf` objects [#1975](https://github.com/r-spatial/sf/pull/1975/)).

* Add a dedicated `dplyr::dplyr_reconstruct()` method for `sftime` objects.
Relying on the method for `sf` objects caused erroneously column binding when the second object was a data frame without conflicting column names for the `sf` and time columns. In this case, a `sf` objects was returned, even though an `sftime` object should be returned. See also https://github.com/r-spatial/sf/issues/1958#issuecomment-1181982244.


* Add methods to convert `sftime` objects from:
+ Objects from the `spatstat` package classes (`ppp`, `psp`, `lpp`)
+ `sftrack` and `sftraj` objects from the `sftrack` package.
Expand Down
1 change: 1 addition & 0 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ register_all_s3_methods <- function() {
register_s3_method("tidyr", "separate_rows", "sftime")
register_s3_method("tidyr", "unite", "sftime")
register_s3_method("tidyr", "unnest", "sftime")
register_s3_method("tidyr", "drop_na", "sftime")

}

Expand Down
15 changes: 14 additions & 1 deletion R/sftime.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ reclass_sftime <- function(x, time_column_name) {
}

#' @name st_sftime
#' @param value An object to insert into \code{x}.
#' @param value An object to insert into \code{x} or with which to rename
#' columns of \code{x}.
#' @examples
#' ## Assigning values to columns
#'
Expand Down Expand Up @@ -298,6 +299,18 @@ reclass_sftime <- function(x, time_column_name) {
structure(NextMethod(), class = c("sftime", setdiff(class(x), "sftime")))
}


##' name st_sftime
##' examples
##' # renaming column names
##' names(x)[1] <- "b"
##'
##' export
#"names<-.sftime" <- function(x, value) {
# out <- NextMethod()
# dplyr_reconstruct.sftime(out, x)
#} # ---todo: raises an error

#### printing ####

#' Helper function to print time columns when printing an \code{sftime} object
Expand Down
20 changes: 20 additions & 0 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @inheritParams sf::tidyverse
#' @inheritParams tidyr::pivot_longer
#' @param x An object of class \code{sftime}.
#' @param y See \code{dplyr::`mutate-joins`}.
#' @param .data An object of class \code{stime}.
#' @return
#' \itemize{
Expand Down Expand Up @@ -310,3 +311,22 @@ dplyr_reconstruct.sftime <- function(data, template) {
}

}

#' @rdname tidyverse
#' @examples
#' ## drop_na
#' x1 %>%
#' mutate(z = c(1, 2, NA)) %>%
#' drop_na(z)
#'
#' x1 %>%
#' mutate(z = c(1, NA, NA)) %>%
#' drop_na(z)
#'
#' x1 %>%
#' mutate(time = replace(time, 1, NA)) %>%
#' drop_na(time)
drop_na.sftime <- function(data, ...) {
reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column"))
}

3 changes: 2 additions & 1 deletion man/st_sftime.Rd

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

91 changes: 45 additions & 46 deletions man/tidyverse.Rd

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

0 comments on commit e82f4ac

Please sign in to comment.