Skip to content

Commit

Permalink
Make drop_na.SpatRaster internal (#13)
Browse files Browse the repository at this point in the history
* Remove examples on drop_na

* Update docs with pkgdev

* Fix docs

* Update docs with pkgdev

Co-authored-by: dieghernan <[email protected]>
  • Loading branch information
dieghernan and dieghernan authored May 19, 2022
1 parent b49a1c6 commit 99bdddd
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 110 deletions.
23 changes: 18 additions & 5 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ references:
given-names: Gábor
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=cli
version: '>= 3.0.0'
- type: software
title: crayon
Expand All @@ -79,7 +80,7 @@ references:
given-names: Gábor
email: [email protected]
year: '2022'
url: https://github.com/r-lib/crayon#readme
url: https://CRAN.R-project.org/package=crayon
- type: software
title: dplyr
abstract: 'dplyr: A Grammar of Data Manipulation'
Expand All @@ -98,6 +99,7 @@ references:
given-names: Kirill
orcid: https://orcid.org/0000-0002-1416-3412
year: '2022'
url: https://CRAN.R-project.org/package=dplyr
- type: software
title: ggplot2
abstract: 'ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics'
Expand Down Expand Up @@ -131,6 +133,7 @@ references:
given-names: Dewey
orcid: https://orcid.org/0000-0002-9415-4582
year: '2022'
url: https://CRAN.R-project.org/package=ggplot2
version: '>= 3.1.0'
- type: software
title: magrittr
Expand All @@ -144,6 +147,7 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=magrittr
- type: software
title: rlang
abstract: 'rlang: Functions for Base Types and Core R and ''Tidyverse'' Features'
Expand All @@ -156,6 +160,7 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=rlang
- type: software
title: scales
abstract: 'scales: Scale Functions for Visualization'
Expand All @@ -167,6 +172,7 @@ references:
- family-names: Seidel
given-names: Dana
year: '2022'
url: https://CRAN.R-project.org/package=scales
- type: software
title: sf
abstract: 'sf: Simple Features for R'
Expand All @@ -177,6 +183,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0001-8049-7069
year: '2022'
url: https://CRAN.R-project.org/package=sf
version: '>= 1.0.0'
- type: software
title: terra
Expand All @@ -188,7 +195,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0001-5872-2872
year: '2022'
url: https://rspatial.org/terra/
url: https://CRAN.R-project.org/package=terra
version: '>= 1.5.0'
- type: software
title: tibble
Expand All @@ -202,6 +209,7 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=tibble
- type: software
title: tidyr
abstract: 'tidyr: Tidy Messy Data'
Expand All @@ -213,6 +221,7 @@ references:
- family-names: Girlich
given-names: Maximilian
year: '2022'
url: https://CRAN.R-project.org/package=tidyr
- type: software
title: isoband
abstract: 'isoband: Generate Isolines and Isobands from Regularly Spaced Elevation
Expand All @@ -228,7 +237,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-5147-4711
year: '2022'
url: https://wilkelab.org/isoband/
url: https://CRAN.R-project.org/package=isoband
- type: software
title: knitr
abstract: 'knitr: A General-Purpose Package for Dynamic Report Generation in R'
Expand All @@ -239,7 +248,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0003-0645-5666
year: '2022'
url: https://yihui.org/knitr/
url: https://CRAN.R-project.org/package=knitr
- type: software
title: lifecycle
abstract: 'lifecycle: Manage the Life Cycle of your Package Functions'
Expand All @@ -253,6 +262,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0003-4757-117X
year: '2022'
url: https://CRAN.R-project.org/package=lifecycle
- type: software
title: maptiles
abstract: 'maptiles: Download and Display Map Tiles'
Expand All @@ -263,7 +273,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-1932-3323
year: '2022'
url: https://github.com/riatelab/maptiles/
url: https://CRAN.R-project.org/package=maptiles
- type: software
title: rmarkdown
abstract: 'rmarkdown: Dynamic Documents for R'
Expand Down Expand Up @@ -302,6 +312,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0003-3925-190X
year: '2022'
url: https://CRAN.R-project.org/package=rmarkdown
- type: software
title: testthat
abstract: 'testthat: Unit Testing for R'
Expand All @@ -311,6 +322,7 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=testthat
version: '>= 3.0.0'
- type: software
title: vdiffr
Expand All @@ -334,3 +346,4 @@ references:
given-names: Vaudor
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=vdiffr
100 changes: 72 additions & 28 deletions R/drop_na.Spat.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#' Drop cells and attributes of Spat* objects containing missing values
#' Drop attributes of SpatVector objects containing missing values
#'
#' @description
#' `r lifecycle::badge('questioning')`
#'
#' `drop_na()` method drops cells and attributes where any layer or column
#' specified by ... contains a missing value.
#' `drop_na()` method drops geometries where any attribute specified by `...`
#' contains a missing value.
#'
#'
#' @return A Spat* object of the same class than `.data`. See **Methods**.
#'
#' @param data A SpatRaster created with [terra::rast()] or a SpatVector
#' created with [terra::vect()].
#' @param ... [`tidy-select`][tidyr::drop_na()] Columns or layers to inspect
#' for missing values. If empty, all columns/layers are used.
#' @param data A SpatVector created with [terra::vect()].
#' @param ... [`tidy-select`][tidyr::drop_na()] Attributes to inspect for
#' missing values. If empty, all attributes are used.
#'
#' @export
#'
Expand All @@ -21,7 +18,72 @@
#'
#' @importFrom tidyr drop_na
#'
#' @seealso [tidyr::drop_na()]
#' @seealso
#' [tidyr::drop_na()]. `r lifecycle::badge('questioning')` A method for
#' SpatRaster is also available, see [drop_na.SpatRaster()].
#'
#' @family tidyr.methods
#'
#'
#' @section Methods:
#'
#' Implementation of the **generic** [tidyr::drop_na()] function.
#'
#' ## SpatVector
#'
#' The implementation of this method is performed on a `by-attribute` basis,
#' meaning that `NAs` are assessed on the attributes (columns) of each vector
#' (rows). The result is a SpatVector with potentially less geometries than the
#' input
#'
#' @examples
#'
#' library(terra)
#'
#' f <- system.file("extdata/cyl.gpkg", package = "tidyterra")
#'
#' v <- terra::vect(f)
#'
#' # Add NAs
#' v <- v %>% mutate(iso2 = ifelse(cpro <= "09", NA, cpro))
#'
#' # Init
#' plot(v, col = "red")
#'
#' # Mask with lyr.1
#' v %>%
#' drop_na(iso2) %>%
#' plot(col = "red")
drop_na.SpatVector <- function(data, ...) {

# Use sf method
sf_obj <- sf::st_as_sf(data)
dropped <- tidyr::drop_na(sf_obj, ...)

return(terra::vect(dropped))
}

#' Drop cells of SpatRaster objects containing missing values
#'
#' @description
#' `r lifecycle::badge('questioning')`. See **Methods**.
#'
#' `drop_na()` method drops cells where any layer specified by `...` contains
#' a missing value.
#'
#' @return A Spat* object of the same class than `.data`. See **Methods**.
#'
#' @param data A SpatRaster created with [terra::rast()].
#' @param ... [`tidy-select`][tidyr::drop_na()] Layers to inspect
#' for missing values. If empty, all layers are used.
#'
#' @export
#' @keywords internal
#' @rdname drop_na.SpatRaster
#'
#' @seealso
#'
#' [tidyr::drop_na()], [drop_na()].
#'
#' @family tidyr.methods
#'
Expand Down Expand Up @@ -56,13 +118,6 @@
#'
#' Check the **Examples** to have a better understanding of this method.
#'
#' ## SpatVector
#'
#' The implementation of this method is performed on a `by-attribute` basis,
#' meaning that `NAs` are assessed on the attributes (columns) of each vector
#' (rows). The result is a SpatVector with potentially less geometries than the
#' input
#'
#' @examples
#'
#' library(terra)
Expand Down Expand Up @@ -136,14 +191,3 @@ drop_na.SpatRaster <- function(data, ...) {

return(newrast)
}

#' @export
#' @rdname drop_na
drop_na.SpatVector <- function(data, ...) {

# Use sf method
sf_obj <- sf::st_as_sf(data)
dropped <- tidyr::drop_na(sf_obj, ...)

return(terra::vect(dropped))
}
3 changes: 2 additions & 1 deletion R/replace_na.Spat.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' Replace `NAs` on layers/attributes with specified values
#'
#'
#' @inheritParams drop_na
#' @param data A SpatRaster created with [terra::rast()] or a SpatVector
#' created with [terra::vect()].
#' @param replace list of values, with one value for each layer/attribute that
#' has `NA` values to be replaced.
#' @param ... Ignored
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
},
"SystemRequirements": null
},
"fileSize": "1431.952KB",
"fileSize": "1433.921KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
Loading

0 comments on commit 99bdddd

Please sign in to comment.