Skip to content

Commit

Permalink
Try best practices with non-standard evaluation (#63)
Browse files Browse the repository at this point in the history
* Try best practices with non-standard evaluation

* Run local tests

* Update docs

* Update docs with pkgdev

Co-authored-by: Diego H <[email protected]>
  • Loading branch information
dieghernan and dieghernan authored Nov 8, 2022
1 parent 11f7217 commit 854c268
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/update-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
run: |
pkgdev::update_docs(precompute = FALSE)
# Regenerate svgs
testthat::test_local()
shell: Rscript {0}

Expand Down
26 changes: 5 additions & 21 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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 @@ -85,7 +84,7 @@ references:
given-names: Gábor
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=crayon
url: https://github.com/r-lib/crayon#readme
- type: software
title: data.table
abstract: 'data.table: Extension of `data.frame`'
Expand All @@ -98,7 +97,6 @@ references:
given-names: Arun
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=data.table
- type: software
title: dplyr
abstract: 'dplyr: A Grammar of Data Manipulation'
Expand All @@ -117,7 +115,6 @@ references:
given-names: Kirill
orcid: https://orcid.org/0000-0002-1416-3412
year: '2022'
url: https://CRAN.R-project.org/package=dplyr
version: '>= 1.0.0'
- type: software
title: ggplot2
Expand Down Expand Up @@ -152,7 +149,6 @@ 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 @@ -166,7 +162,6 @@ 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 @@ -179,7 +174,6 @@ 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 @@ -191,7 +185,6 @@ 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 @@ -202,7 +195,6 @@ 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 @@ -214,7 +206,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0001-5872-2872
year: '2022'
url: https://CRAN.R-project.org/package=terra
url: https://rspatial.org/terra/
version: '>= 1.5-12'
- type: software
title: tibble
Expand All @@ -228,7 +220,6 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=tibble
version: '>= 3.0.0'
- type: software
title: tidyr
Expand All @@ -241,7 +232,6 @@ references:
- family-names: Girlich
given-names: Maximilian
year: '2022'
url: https://CRAN.R-project.org/package=tidyr
version: '>= 1.0.0'
- type: software
title: isoband
Expand All @@ -258,7 +248,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-5147-4711
year: '2022'
url: https://CRAN.R-project.org/package=isoband
url: https://wilkelab.org/isoband/
- type: software
title: knitr
abstract: 'knitr: A General-Purpose Package for Dynamic Report Generation in R'
Expand All @@ -269,7 +259,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0003-0645-5666
year: '2022'
url: https://CRAN.R-project.org/package=knitr
url: https://yihui.org/knitr/
- type: software
title: lifecycle
abstract: 'lifecycle: Manage the Life Cycle of your Package Functions'
Expand All @@ -283,7 +273,6 @@ 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 @@ -294,7 +283,7 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-1932-3323
year: '2022'
url: https://CRAN.R-project.org/package=maptiles
url: https://github.com/riatelab/maptiles/
- type: software
title: rmarkdown
abstract: 'rmarkdown: Dynamic Documents for R'
Expand Down Expand Up @@ -333,7 +322,6 @@ 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: s2
abstract: 's2: Spherical Geometry Operators Using the S2 Geometry Library'
Expand All @@ -351,7 +339,6 @@ references:
given-names: Ege
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=s2
- type: software
title: testthat
abstract: 'testthat: Unit Testing for R'
Expand All @@ -361,7 +348,6 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=testthat
version: '>= 3.0.0'
- type: software
title: tidyverse
Expand All @@ -372,7 +358,6 @@ references:
given-names: Hadley
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=tidyverse
- type: software
title: vdiffr
abstract: 'vdiffr: Visual Regression Testing and Graphical Diffing'
Expand All @@ -395,4 +380,3 @@ references:
given-names: Vaudor
email: [email protected]
year: '2022'
url: https://CRAN.R-project.org/package=vdiffr
7 changes: 2 additions & 5 deletions R/geom_spat_contour.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,12 @@ geom_spatraster_contour <- function(mapping = NULL, data,


# 1. Work with aes ----
spatraster <- NULL
lyr <- NULL

mapping <- override_aesthetics(
mapping,
ggplot2::aes(
spatraster = spatraster,
spatraster = .data$spatraster,
# For faceting
lyr = lyr
lyr = .data$lyr
)
)

Expand Down
7 changes: 2 additions & 5 deletions R/geom_spat_contour_fill.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ geom_spatraster_contour_filled <- function(mapping = NULL, data,


# 1. Work with aes ----
spatraster <- NULL
lyr <- NULL

mapping <- override_aesthetics(
mapping,
ggplot2::aes(
spatraster = spatraster,
spatraster = .data$spatraster,
# For faceting
lyr = lyr
lyr = .data$lyr
)
)

Expand Down
16 changes: 5 additions & 11 deletions R/geom_spatraster.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,13 @@ prepare_aes_spatraster <- function(mapping = aes(),
# Prepare aes for StatTerraSpatRaster
mapinit <- cleanup_aesthetics(mapping, "group")

spatraster <- NULL
lyr <- NULL

mapinit <- override_aesthetics(
mapinit,
ggplot2::aes(
spatraster = spatraster,
spatraster = .data$spatraster,
# For faceting
lyr = lyr,
group = lyr
lyr = .data$lyr,
group = .data$lyr
)
)

Expand All @@ -462,12 +459,10 @@ prepare_aes_spatraster <- function(mapping = aes(),
fill_not_provided <- is.na(fill_from_aes)
is_layer <- fill_from_aes %in% raster_names


value <- NULL
# If not provided add after_stat
if (fill_not_provided) {
map_not_prov <- override_aesthetics(
mapinit, ggplot2::aes(fill = after_stat(value))
mapinit, ggplot2::aes(fill = after_stat(.data$value))
)

result_obj$map <- map_not_prov
Expand All @@ -476,9 +471,8 @@ prepare_aes_spatraster <- function(mapping = aes(),

# If it is a layer need to override the fill value and keep the namelayer
if (is_layer) {
value <- NULL
map_layer <- override_aesthetics(
ggplot2::aes(fill = after_stat(value)),
ggplot2::aes(fill = after_stat(.data$value)),
mapinit
)

Expand Down
3 changes: 1 addition & 2 deletions R/geom_spatraster_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ geom_spatraster_rgb <- function(mapping = aes(),
}

# 1. Work with aes ----
spatraster <- NULL
mapping <- override_aesthetics(
mapping,
ggplot2::aes(
spatraster = spatraster
spatraster = .data$spatraster
)
)

Expand Down
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ objects with [{ggplot2}](https://ggplot2.tidyverse.org/).
{tidyverse} methods implemented on {tidyterra} works differently
depending on the type of Spat\* object:

- SpatVector: the methods are implemented taking advantage of the
tidyverse implementation on {sf}. The SpatVector object is converted
first to sf via `sf::st_as_sf()`, then the method (or function) is
applied and finally the object is converted back to SpatVector with
`terra::vect()`. Hence, rows correspond to geometries and columns
correspond to attributes of the geometry.

- SpatRaster: The implementation on SpatRaster objects differs, since
the methods could be applied to layers or to cells. {tidyterra}
overall approach is to treat the layers as columns of a tibble and
the cells as rows (i.e. `select(SpatRaster, 1)` would select the
first layer of a SpatRaster).
- SpatVector: the methods are implemented taking advantage of the
tidyverse implementation on {sf}. The SpatVector object is converted
first to sf via `sf::st_as_sf()`, then the method (or function) is
applied and finally the object is converted back to SpatVector with
`terra::vect()`. Hence, rows correspond to geometries and columns
correspond to attributes of the geometry.

- SpatRaster: The implementation on SpatRaster objects differs, since
the methods could be applied to layers or to cells. {tidyterra}
overall approach is to treat the layers as columns of a tibble and the
cells as rows (i.e. `select(SpatRaster, 1)` would select the first
layer of a SpatRaster).

The methods implemented return the same type of object used as input,
unless the expected behavior of the method is to return another type of
Expand Down Expand Up @@ -100,6 +100,7 @@ install.packages("tidyterra")
You can install the development version of {tidyterra} like so:

``` r

remotes::install_github("dieghernan/tidyterra")
```

Expand Down Expand Up @@ -149,6 +150,7 @@ ggplot() +

``` r


# Create maximum differences

variation <- rastertemp %>%
Expand Down Expand Up @@ -184,6 +186,7 @@ ggplot(prov) +
{ggplot2}

``` r

f_tile <- system.file("extdata/cyl_tile.tif", package = "tidyterra")

rgb_tile <- rast(f_tile)
Expand All @@ -201,6 +204,7 @@ plot
<img src="https://raw.githubusercontent.com/dieghernan/tidyterra/main/img/README-example-tile-1.png" width="100%" />

``` r

# Recognizes coord_sf()
plot +
# Change crs and datum (for relabeling graticules)
Expand All @@ -213,6 +217,7 @@ plot +
{ggplot2}:

``` r

asia <- rast(system.file("extdata/asia.tif", package = "tidyterra"))

terra::plot(asia)
Expand All @@ -221,6 +226,7 @@ terra::plot(asia)
<img src="https://raw.githubusercontent.com/dieghernan/tidyterra/main/img/README-hypso-1.png" width="100%" />

``` r

ggplot() +
geom_spatraster(data = asia) +
scale_fill_hypso_tint_c(
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
},
"SystemRequirements": null
},
"fileSize": "2550.418KB",
"fileSize": "2553.946KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down

0 comments on commit 854c268

Please sign in to comment.