Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate ggplot2 scales, closes #164 #294

Merged
merged 7 commits into from
Dec 30, 2021
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
93 changes: 59 additions & 34 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ jobs:
build:
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') }}
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
name: check on ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'devel', dispatch: 'yes'}
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release', covr: 'yes'}
- {os: ubuntu-latest, r: 'oldrel'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
_R_CHECK_CRAN_INCOMING_REMOTE_: false
_R_CHECK_FORCE_SUGGESTS_: false
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- name: Configure git
Expand All @@ -32,38 +31,22 @@ jobs:
- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true
- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies=TRUE), ".github/depends.Rds", version=2)
shell: Rscript {0}

- name: Cache R packages
if: ${{ runner.os != 'Windows' }}
uses: actions/cache@v1
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ matrix.config.os }}-r-${{ matrix.config.r }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ matrix.config.os }}-r-${{ matrix.config.r }}-1-

- name: Install Linux dependencies
if: ${{ runner.os == 'Linux' }}
run: sudo apt-get install devscripts libcurl4-openssl-dev libudunits2-dev

- name: Install macOS dependencies
if: ${{ runner.os == 'macOS' }}
run: brew install automake udunits

- name: Install dependencies
run: |
remotes::install_deps(dependencies=TRUE)
remotes::install_cran(c("rcmdcheck", "covr"))
shell: Rscript {0}
- uses: r-lib/actions/setup-r-dependencies@v1
with:
extra-packages: rcmdcheck, covr

- name: Check
run: rcmdcheck::rcmdcheck(args=c("--no-manual", "--as-cran"), error_on="warning", check_dir="check")
run: |
rcmdcheck::rcmdcheck(args=c("--no-manual", "--as-cran"),
error_on="warning", check_dir="check")
shell: Rscript {0}

- name: Upload check results
Expand All @@ -78,10 +61,52 @@ jobs:
run: covr::codecov()
shell: Rscript {0}

- name: Dispatch quantities
if: ${{ success() && github.ref == 'refs/heads/master' && matrix.config.dispatch == 'yes' }}
uses: peter-evans/repository-dispatch@v1
quantities:
needs: build
runs-on: ${{ matrix.config.os }}
name: quantities on ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
_R_CHECK_CRAN_INCOMING_REMOTE_: false

steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.QUANTITIES_TOKEN }}
repository: r-quantities/quantities
event-type: units
- name: Add units and errors as Remotes
run: |
echo "Remotes: r-quantities/errors," >> DESCRIPTION
echo " $GITHUB_REPOSITORY@$GITHUB_SHA" >> DESCRIPTION
- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true
- uses: r-lib/actions/setup-pandoc@v1

- uses: r-lib/actions/setup-r-dependencies@v1
with:
extra-packages: rcmdcheck

- name: Check
run: |
rcmdcheck::rcmdcheck(args=c("--no-manual", "--as-cran"),
error_on="warning", check_dir="check")
shell: Rscript {0}

- name: Upload check results
if: ${{ failure() }}
uses: actions/upload-artifact@master
with:
name: ${{ matrix.config.os }}-r${{ matrix.config.r }}-results
path: check
85 changes: 0 additions & 85 deletions .github/workflows/quantities.yml

This file was deleted.

9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.p
person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"),
person("Tomasz", "Kalinowski", role = "aut"),
person("James", "Hiebert", role = "ctb"),
person("Iñaki", "Ucar", role = "aut", email = "iucar@fedoraproject.org", comment = c(ORCID = "0000-0001-6403-5550"))
person("Iñaki", "Ucar", role = "aut", email = "iucar@fedoraproject.org", comment = c(ORCID = "0000-0001-6403-5550")),
person("Thomas Lin", "Pedersen", role = "ctb")
)
Depends:
R (>= 3.0.2)
Expand All @@ -22,10 +23,10 @@ Suggests:
pillar (>= 1.3.0),
dplyr (>= 1.0.0),
vctrs (>= 0.3.1),
knitr,
ggplot2 (> 3.2.1),
testthat (>= 3.0.0),
ggforce,
ggplot2 (>= 2.2.2),
vdiffr,
knitr,
rmarkdown
VignetteBuilder: knitr
Description: Support for measurement units in R vectors, matrices
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export(make_unit_label)
export(make_units)
export(mixed_units)
export(remove_unit)
export(scale_x_units)
export(scale_y_units)
export(set_units)
export(ud_are_convertible)
export(unitless)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

* fix multipliers for round trip log-exp operations; #292

* integrate `ggplot2` scales (previously in the `ggforce` package) to
automatically print axes with units; #294 addressing #164

# version 0.7-2

* enhance `pillar` integration; #273, #275, #280 @krlmlr
Expand Down
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ NULL
packageStartupMessage(.startup_msg(TRUE))
}

.onUnload = function(libname, pkgname) {
.onUnload = function(libpath) {
ud_exit()
}
112 changes: 112 additions & 0 deletions R/scale_units.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#' Position scales for units data
#'
#' These are the default scales for the \code{units} class. These will usually
#' be added automatically. To override manually, use \code{scale_*_units}.
#'
#' @param ... arguments passed on to \code{\link[ggplot2]{continuous_scale}}.
#' @inheritParams ggplot2::continuous_scale
#'
#' @param unit A unit specification to use for the axis. If given, the values
#' will be converted to this unit before plotting. An error will be thrown if
#' the specified unit is incompatible with the unit of the data.
#'
#' @name scale_units
#' @aliases NULL
#'
#' @examples
#' if (requireNamespace("ggplot2", quietly=TRUE)) {
#'
#' library(ggplot2)
#'
#' mtcars$consumption <- set_units(mtcars$mpg, mi / gallon)
#' mtcars$power <- set_units(mtcars$hp, hp)
#'
#' # Use units encoded into the data
#' ggplot(mtcars) +
#' geom_point(aes(power, consumption))
#'
#' # Convert units on the fly during plotting
#' ggplot(mtcars) +
#' geom_point(aes(power, consumption)) +
#' scale_x_units(unit = "W") +
#' scale_y_units(unit = "km/l")
#'
#' # Resolve units when transforming data
#' ggplot(mtcars) +
#' geom_point(aes(power, 1 / consumption))
#'
#' }
NULL

#' @rdname scale_units
#' @export
scale_x_units <- function(..., position = "bottom", unit = NULL) {
if (!requireNamespace("ggplot2", quietly=TRUE))
stop("package 'ggplot2' is required for this functionality", call.=FALSE)

sc <- ggplot2::continuous_scale(
c("x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final",
"xlower", "xmiddle", "xupper"),
"position_c", identity, ...,
position = position,
guide = ggplot2::waiver(),
super = MakeScaleContinuousPositionUnits()
)
sc$units <- as_units(unit)
sc
}

#' @rdname scale_units
#' @export
scale_y_units <- function(..., unit = NULL) {
if (!requireNamespace("ggplot2", quietly=TRUE))
stop("package 'ggplot2' is required for this functionality", call.=FALSE)

sc <- ggplot2::continuous_scale(
c("y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final",
"lower", "middle", "upper"),
"position_c", identity, ...,
guide = ggplot2::waiver(),
super = MakeScaleContinuousPositionUnits()
)
sc$units <- as_units(unit)
sc
}

MakeScaleContinuousPositionUnits <- function() {
ggplot2::ggproto(
"ScaleContinuousPositionUnits",
ggplot2::ScaleContinuousPosition,
units = NULL,

train = function(self, x) {
if (length(x) == 0) return()
if (!is.null(self$units))
units(x) <- as_units(1, self$units)
self$range$train(x)
},

map = function(self, x, limits = self$get_limits()) {
if (inherits(x, "units")) {
if (is.null(self$units))
self$units <- units(x)
else units(x) <- as_units(1, self$units)
x <- drop_units(x)
}
ggplot2::ggproto_parent(
ggplot2::ScaleContinuousPosition, self)$map(x, limits)
},

make_title = function(self, title) {
make_unit_label(title, as_units(1, self$units))
}
)
}

# registered in .onLoad()
scale_type.units <- function(x) {
if (!"units" %in% .packages())
stop("Variable of class 'units' found, but 'units' package is not attached.\n",
" Please, attach it using 'library(units)' to properly show scales with units.")
c("units", "continuous")
}
1 change: 1 addition & 0 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ register_all_s3_methods <- function() {
register_s3_method("vctrs::vec_restore", "units")
register_s3_method("vctrs::vec_ptype2", "units.units")
register_s3_method("vctrs::vec_cast", "units.units")
register_s3_method("ggplot2::scale_type", "units")
}

register_s3_method <- function(generic, class, fun=NULL) {
Expand Down
2 changes: 1 addition & 1 deletion demo/00Index
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cf checks that the NetCDF-CF units are all udunits compatible
ggforce demonstrates automatic units in default axis labels in ggplot2
ggplot2 demonstrates automatic units in default axis labels in ggplot2
year illustrate the year lenghts, for different definitions of year
Loading