diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml
index ee65ccb5..064677b4 100644
--- a/.github/workflows/R-CMD-check.yaml
+++ b/.github/workflows/R-CMD-check.yaml
@@ -10,7 +10,9 @@ on:
pull_request:
branches: [main, master]
-name: R-CMD-check
+name: R-CMD-check.yaml
+
+permissions: read-all
jobs:
R-CMD-check:
@@ -25,24 +27,22 @@ jobs:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- # Use 3.6 to trigger usage of RTools35
- - {os: windows-latest, r: '3.6'}
- # use 4.1 to check with rtools40's older compiler
- - {os: windows-latest, r: '4.1'}
-
- - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- - {os: ubuntu-latest, r: 'release'}
- - {os: ubuntu-latest, r: 'oldrel-1'}
- - {os: ubuntu-latest, r: 'oldrel-2'}
- - {os: ubuntu-latest, r: 'oldrel-3'}
- - {os: ubuntu-latest, r: 'oldrel-4'}
+ # use 4.0 or 4.1 to check with rtools40's older compiler
+ - {os: windows-latest, r: 'oldrel-4'}
+
+ - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
+ - {os: ubuntu-latest, r: 'release'}
+ - {os: ubuntu-latest, r: 'oldrel-1'}
+ - {os: ubuntu-latest, r: 'oldrel-2'}
+ - {os: ubuntu-latest, r: 'oldrel-3'}
+ - {os: ubuntu-latest, r: 'oldrel-4'}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
steps:
- - uses: actions/checkout@v3
+ - uses: actions/checkout@v4
- uses: r-lib/actions/setup-pandoc@v2
@@ -60,3 +60,4 @@ jobs:
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
+ build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml
index ed7650c7..4bbce750 100644
--- a/.github/workflows/pkgdown.yaml
+++ b/.github/workflows/pkgdown.yaml
@@ -9,7 +9,9 @@ on:
types: [published]
workflow_dispatch:
-name: pkgdown
+name: pkgdown.yaml
+
+permissions: read-all
jobs:
pkgdown:
@@ -22,7 +24,7 @@ jobs:
permissions:
contents: write
steps:
- - uses: actions/checkout@v3
+ - uses: actions/checkout@v4
- uses: r-lib/actions/setup-pandoc@v2
@@ -41,7 +43,7 @@ jobs:
- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
- uses: JamesIves/github-pages-deploy-action@v4.4.1
+ uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
diff --git a/.github/workflows/pr-commands.yaml b/.github/workflows/pr-commands.yaml
index 71f335b3..2edd93f2 100644
--- a/.github/workflows/pr-commands.yaml
+++ b/.github/workflows/pr-commands.yaml
@@ -4,7 +4,9 @@ on:
issue_comment:
types: [created]
-name: Commands
+name: pr-commands.yaml
+
+permissions: read-all
jobs:
document:
@@ -13,8 +15,10 @@ jobs:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
+ permissions:
+ contents: write
steps:
- - uses: actions/checkout@v3
+ - uses: actions/checkout@v4
- uses: r-lib/actions/pr-fetch@v2
with:
@@ -50,8 +54,10 @@ jobs:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
+ permissions:
+ contents: write
steps:
- - uses: actions/checkout@v3
+ - uses: actions/checkout@v4
- uses: r-lib/actions/pr-fetch@v2
with:
diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml
index 27d45283..98822609 100644
--- a/.github/workflows/test-coverage.yaml
+++ b/.github/workflows/test-coverage.yaml
@@ -6,7 +6,9 @@ on:
pull_request:
branches: [main, master]
-name: test-coverage
+name: test-coverage.yaml
+
+permissions: read-all
jobs:
test-coverage:
@@ -15,7 +17,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- - uses: actions/checkout@v3
+ - uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
with:
@@ -23,28 +25,37 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
- extra-packages: any::covr
+ extra-packages: any::covr, any::xml2
needs: coverage
- name: Test coverage
run: |
- covr::codecov(
+ cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
+ covr::to_cobertura(cov)
shell: Rscript {0}
+ - uses: codecov/codecov-action@v4
+ with:
+ fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
+ file: ./cobertura.xml
+ plugin: noop
+ disable_search: true
+ token: ${{ secrets.CODECOV_TOKEN }}
+
- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
- find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
+ find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash
- name: Upload test results
if: failure()
- uses: actions/upload-artifact@v3
+ uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
diff --git a/DESCRIPTION b/DESCRIPTION
index 3800da3c..38a3ec23 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -2,7 +2,7 @@ Package: scales
Title: Scale Functions for Visualization
Version: 1.3.0.9000
Authors@R: c(
- person("Hadley", "Wickham", , "hadley@posit.co", role = c("aut")),
+ person("Hadley", "Wickham", , "hadley@posit.co", role = "aut"),
person("Thomas Lin", "Pedersen", , "thomas.pedersen@posit.co", role = c("cre", "aut"),
comment = c(ORCID = "0000-0002-5147-4711")),
person("Dana", "Seidel", role = "aut"),
@@ -14,7 +14,7 @@ License: MIT + file LICENSE
URL: https://scales.r-lib.org, https://github.com/r-lib/scales
BugReports: https://github.com/r-lib/scales/issues
Depends:
- R (>= 3.6)
+ R (>= 4.0)
Imports:
cli,
farver (>= 2.0.3),
@@ -35,6 +35,7 @@ Suggests:
testthat (>= 3.0.0)
Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
+Config/usethis/last-upkeep: 2024-10-24
Encoding: UTF-8
LazyLoad: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
diff --git a/LICENSE b/LICENSE
index 5353308f..ae8f3606 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,2 +1,2 @@
-YEAR: 2023
+YEAR: 2024
COPYRIGHT HOLDER: scales authors
diff --git a/LICENSE.md b/LICENSE.md
index 86d1cefa..aa15f5c2 100644
--- a/LICENSE.md
+++ b/LICENSE.md
@@ -1,6 +1,6 @@
# MIT License
-Copyright (c) 2023 scales authors
+Copyright (c) 2024 scales authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/NAMESPACE b/NAMESPACE
index a3328b77..13b7e9bc 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -48,6 +48,9 @@ S3method(rescale_mid,logical)
S3method(rescale_mid,numeric)
S3method(round_any,POSIXct)
S3method(round_any,numeric)
+S3method(toPaletteFunc,"function")
+S3method(toPaletteFunc,character)
+S3method(toPaletteFunc,matrix)
export(ContinuousRange)
export(DiscreteRange)
export(Range)
diff --git a/R/colour-mapping.R b/R/colour-mapping.R
index 8ed05d0e..5402df49 100644
--- a/R/colour-mapping.R
+++ b/R/colour-mapping.R
@@ -318,6 +318,7 @@ toPaletteFunc <- function(pal, alpha, nlevels) {
# Strings are interpreted as color names, unless length is 1 and it's the name
# of an RColorBrewer palette that is marked as qualitative
+#' @export
toPaletteFunc.character <- function(pal, alpha, nlevels) {
if (length(pal) == 1 && pal %in% row.names(RColorBrewer::brewer.pal.info)) {
paletteInfo <- RColorBrewer::brewer.pal.info[pal, ]
@@ -338,11 +339,13 @@ toPaletteFunc.character <- function(pal, alpha, nlevels) {
}
# Accept colorRamp style matrix
+#' @export
toPaletteFunc.matrix <- function(pal, alpha, nlevels) {
toPaletteFunc(farver::decode_colour(pal), alpha = alpha)
}
# If a function, just assume it's already a function over [0-1]
+#' @export
toPaletteFunc.function <- function(pal, alpha, nlevels) {
pal
}
diff --git a/R/label-number.R b/R/label-number.R
index b8a06eca..d6e28c28 100644
--- a/R/label-number.R
+++ b/R/label-number.R
@@ -469,7 +469,7 @@ cut_time_scale <- function(space = FALSE) {
out <- c(
0,
"ns" = 1e-9,
- "\u03BCs" = 1e-6,
+ "us" = 1e-6,
"ms" = 1e-3,
"s" = 1,
"m" = 60,
@@ -477,6 +477,9 @@ cut_time_scale <- function(space = FALSE) {
"d" = 24 * 3600,
"w" = 7 * 24 * 3600
)
+ if (l10n_info()[["UTF-8"]]) {
+ names(out)[3] <- "\u03BCs"
+ }
if (space) {
names(out) <- paste0(" ", names(out))
}
diff --git a/README.Rmd b/README.Rmd
index d7d1b6fa..3efcba45 100644
--- a/README.Rmd
+++ b/README.Rmd
@@ -4,7 +4,8 @@ output: github_document
-```{r, echo = FALSE}
+```{r}
+#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
@@ -15,18 +16,19 @@ knitr::opts_chunk$set(
```
# scales
-
+
[![CRAN status](https://www.r-pkg.org/badges/version/scales)](https://CRAN.R-project.org/package=scales)
[![R-CMD-check](https://github.com/r-lib/scales/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/scales/actions/workflows/R-CMD-check.yaml)
-[![Codecov test coverage](https://codecov.io/gh/r-lib/scales/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/scales?branch=main)
+[![Codecov test coverage](https://codecov.io/gh/r-lib/scales/graph/badge.svg)](https://app.codecov.io/gh/r-lib/scales)
One of the most difficult parts of any graphics package is scaling, converting from data values to perceptual properties. The inverse of scaling, making guides (legends and axes) that can be used to read the graph, is often even harder! The scales packages provides the internal scaling infrastructure used by [ggplot2](https://ggplot2.tidyverse.org/), and gives you tools to override the default breaks, labels, transformations and palettes.
## Installation
-```{r, eval = FALSE}
+```{r}
+#| eval: false
# Scales is installed when you install ggplot2 or the tidyverse.
# But you can install just scales from CRAN:
install.packages("scales")
@@ -35,45 +37,61 @@ install.packages("scales")
# install.packages("pak")
pak::pak("r-lib/scales")
```
-
+
## Usage
### Breaks and labels
The most common use of the scales package is to control the appearance of axis and legend labels. Use a `break_` function to control how breaks are generated from the limits, and a `label_` function to control how breaks are turned in to labels.
-```{r labels}
+```{r}
+#| label: labels
+#| fig-alt: >
+#| A line plot created with ggplot2, showing property sales in Texas. The x scale
+#| uses `scales::break_width()` to place breaks every second year, and
+#| `scales::label_date()` to create a custom format for the labels. The y-scale
+#| uses `scales::label_number()` to reformat the labels with
+#| `scales::cut_short_scale()`.
library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
-txhousing %>%
- mutate(date = make_date(year, month, 1)) %>%
- group_by(city) %>%
- filter(min(sales) > 5e2) %>%
- ggplot(aes(date, sales, group = city)) +
- geom_line(na.rm = TRUE) +
+txhousing %>%
+ mutate(date = make_date(year, month, 1)) %>%
+ group_by(city) %>%
+ filter(min(sales) > 5e2) %>%
+ ggplot(aes(date, sales, group = city)) +
+ geom_line(na.rm = TRUE) +
scale_x_date(
NULL,
- breaks = scales::breaks_width("2 years"),
+ breaks = scales::breaks_width("2 years"),
labels = scales::label_date("'%y")
- ) +
+ ) +
scale_y_log10(
"Total sales",
labels = scales::label_number(scale_cut = scales::cut_short_scale())
)
+```
-economics %>%
- filter(date < ymd("1970-01-01")) %>%
- ggplot(aes(date, pce)) +
- geom_line() +
+```{r}
+#| fig-alt: >
+#| A line plot created with ggplot2, showing personal expenses between 1967 and
+#| 1970. The x axis uses `scales::break_width()` to put a break every 3 months
+#| and `scales::label_date_short()` to only show the year on the first occuring
+#| break of that year. The y axis uses `scales::breaks_extended()` to request
+#| 8 breaks, though only 6 are ultimately provided, and
+#| `scales::label_dollar()` to format the label as a dollar value.
+economics %>%
+ filter(date < ymd("1970-01-01")) %>%
+ ggplot(aes(date, pce)) +
+ geom_line() +
scale_x_date(NULL,
- breaks = scales::breaks_width("3 months"),
+ breaks = scales::breaks_width("3 months"),
labels = scales::label_date_short()
- ) +
+ ) +
scale_y_continuous("Personal consumption expenditures",
breaks = scales::breaks_extended(8),
- labels = scales::label_dollar()
+ labels = scales::label_dollar()
)
```
@@ -83,7 +101,13 @@ Generally, I don't recommend running `library(scales)` because when you type (e.
Scales colour palettes are used to power the scales in ggplot2, but you can use them in any plotting system. The following example shows how you might apply them to a base plot.
-```{r, palettes}
+```{r}
+#| label: palettes
+#| fig-alt: >
+#| A scatterplot created with base plot showing the relationship between
+#| sepal length and sepal width in the Iris dataset. The points are coloured
+#| according to species and the `scales::pal_brewer()` are used to provide the
+#| colours.
library(scales)
# pull a list of colours from any palette
pal_viridis()(4)
@@ -94,10 +118,15 @@ par(mar = c(5, 5, 1, 1))
plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20)
```
-scales also gives users the ability to define and apply their own custom
+scales also gives users the ability to define and apply their own custom
transformation functions for repeated use.
-```{r transforms}
+```{r}
+#| label: transforms
+#| fig-alt: >
+#| A scatterplot created with ggplot2 showing the relationship between diamond
+#| price and its carat for a subset of the data in the diamonds dataset. The
+#| y scale uses a custom log transform created with `scales::new_transform()`.
# use new_transform to build a new transformation
transform_logp3 <- new_transform(
name = "logp",
@@ -108,6 +137,6 @@ transform_logp3 <- new_transform(
dsamp <- sample_n(diamonds, 100)
ggplot(dsamp, aes(carat, price, colour = color)) +
- geom_point() +
+ geom_point() +
scale_y_continuous(trans = transform_logp3)
```
diff --git a/README.md b/README.md
index 394b3663..af518243 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@
status](https://www.r-pkg.org/badges/version/scales)](https://CRAN.R-project.org/package=scales)
[![R-CMD-check](https://github.com/r-lib/scales/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/scales/actions/workflows/R-CMD-check.yaml)
[![Codecov test
-coverage](https://codecov.io/gh/r-lib/scales/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/scales?branch=main)
+coverage](https://codecov.io/gh/r-lib/scales/graph/badge.svg)](https://app.codecov.io/gh/r-lib/scales)
One of the most difficult parts of any graphics package is scaling,
@@ -46,42 +46,41 @@ library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
-txhousing %>%
- mutate(date = make_date(year, month, 1)) %>%
- group_by(city) %>%
- filter(min(sales) > 5e2) %>%
- ggplot(aes(date, sales, group = city)) +
- geom_line(na.rm = TRUE) +
+txhousing %>%
+ mutate(date = make_date(year, month, 1)) %>%
+ group_by(city) %>%
+ filter(min(sales) > 5e2) %>%
+ ggplot(aes(date, sales, group = city)) +
+ geom_line(na.rm = TRUE) +
scale_x_date(
NULL,
- breaks = scales::breaks_width("2 years"),
+ breaks = scales::breaks_width("2 years"),
labels = scales::label_date("'%y")
- ) +
+ ) +
scale_y_log10(
"Total sales",
labels = scales::label_number(scale_cut = scales::cut_short_scale())
)
```
-![](man/figures/README-labels-1.png)
+
``` r
-
-economics %>%
- filter(date < ymd("1970-01-01")) %>%
- ggplot(aes(date, pce)) +
- geom_line() +
+economics %>%
+ filter(date < ymd("1970-01-01")) %>%
+ ggplot(aes(date, pce)) +
+ geom_line() +
scale_x_date(NULL,
- breaks = scales::breaks_width("3 months"),
+ breaks = scales::breaks_width("3 months"),
labels = scales::label_date_short()
- ) +
+ ) +
scale_y_continuous("Personal consumption expenditures",
breaks = scales::breaks_extended(8),
- labels = scales::label_dollar()
+ labels = scales::label_dollar()
)
```
-![](man/figures/README-labels-2.png)
+
Generally, I don’t recommend running `library(scales)` because when you
type (e.g.) `scales::label_` autocomplete will provide you with a list
@@ -94,6 +93,9 @@ can use them in any plotting system. The following example shows how you
might apply them to a base plot.
``` r
+#|Â sepal length and sepal width in the Iris dataset. The points are coloured
+#| according to species and the `scales::pal_brewer()` are used to provide the
+#| colours.
library(scales)
# pull a list of colours from any palette
pal_viridis()(4)
@@ -105,7 +107,7 @@ par(mar = c(5, 5, 1, 1))
plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20)
```
-![](man/figures/README-palettes-1.png)
+
scales also gives users the ability to define and apply their own custom
transformation functions for repeated use.
@@ -121,8 +123,8 @@ transform_logp3 <- new_transform(
dsamp <- sample_n(diamonds, 100)
ggplot(dsamp, aes(carat, price, colour = color)) +
- geom_point() +
+ geom_point() +
scale_y_continuous(trans = transform_logp3)
```
-![](man/figures/README-transforms-1.png)
+
diff --git a/man/figures/README-transforms-1.png b/man/figures/README-transforms-1.png
index f6e5495d..28c96577 100644
Binary files a/man/figures/README-transforms-1.png and b/man/figures/README-transforms-1.png differ
diff --git a/man/figures/README-unnamed-chunk-4-1.png b/man/figures/README-unnamed-chunk-4-1.png
new file mode 100644
index 00000000..3f02ded1
Binary files /dev/null and b/man/figures/README-unnamed-chunk-4-1.png differ
diff --git a/man/figures/logo.png b/man/figures/logo.png
index b9fbd637..b9fd152b 100644
Binary files a/man/figures/logo.png and b/man/figures/logo.png differ
diff --git a/man/label_dictionary.Rd b/man/label_dictionary.Rd
index ded7a697..7731c13a 100644
--- a/man/label_dictionary.Rd
+++ b/man/label_dictionary.Rd
@@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/label-lut.R
+% Please edit documentation in R/label-dictionary.R
\name{label_dictionary}
\alias{label_dictionary}
\title{Labels from lookup tables}
diff --git a/pkgdown/favicon/apple-touch-icon.png b/pkgdown/favicon/apple-touch-icon.png
index f9ef2efe..15ee19d1 100644
Binary files a/pkgdown/favicon/apple-touch-icon.png and b/pkgdown/favicon/apple-touch-icon.png differ
diff --git a/pkgdown/favicon/favicon-48x48.png b/pkgdown/favicon/favicon-48x48.png
new file mode 100644
index 00000000..cc543dc2
Binary files /dev/null and b/pkgdown/favicon/favicon-48x48.png differ
diff --git a/pkgdown/favicon/favicon.ico b/pkgdown/favicon/favicon.ico
index 90913fb5..91ab52d4 100644
Binary files a/pkgdown/favicon/favicon.ico and b/pkgdown/favicon/favicon.ico differ
diff --git a/pkgdown/favicon/favicon.svg b/pkgdown/favicon/favicon.svg
new file mode 100644
index 00000000..3d8b8318
--- /dev/null
+++ b/pkgdown/favicon/favicon.svg
@@ -0,0 +1,1205 @@
+
\ No newline at end of file
diff --git a/pkgdown/favicon/site.webmanifest b/pkgdown/favicon/site.webmanifest
new file mode 100644
index 00000000..4ebda26b
--- /dev/null
+++ b/pkgdown/favicon/site.webmanifest
@@ -0,0 +1,21 @@
+{
+ "name": "",
+ "short_name": "",
+ "icons": [
+ {
+ "src": "/web-app-manifest-192x192.png",
+ "sizes": "192x192",
+ "type": "image/png",
+ "purpose": "maskable"
+ },
+ {
+ "src": "/web-app-manifest-512x512.png",
+ "sizes": "512x512",
+ "type": "image/png",
+ "purpose": "maskable"
+ }
+ ],
+ "theme_color": "#ffffff",
+ "background_color": "#ffffff",
+ "display": "standalone"
+}
\ No newline at end of file
diff --git a/pkgdown/favicon/web-app-manifest-192x192.png b/pkgdown/favicon/web-app-manifest-192x192.png
new file mode 100644
index 00000000..2ba94d33
Binary files /dev/null and b/pkgdown/favicon/web-app-manifest-192x192.png differ
diff --git a/pkgdown/favicon/web-app-manifest-512x512.png b/pkgdown/favicon/web-app-manifest-512x512.png
new file mode 100644
index 00000000..0ec6c26f
Binary files /dev/null and b/pkgdown/favicon/web-app-manifest-512x512.png differ
diff --git a/tests/testthat/_snaps/colour-mapping.md b/tests/testthat/_snaps/colour-mapping.md
index 60764024..0e929a45 100644
--- a/tests/testthat/_snaps/colour-mapping.md
+++ b/tests/testthat/_snaps/colour-mapping.md
@@ -1,3 +1,80 @@
+# Outside of domain returns na.color
+
+ Code
+ col_factor(bw, letters, na.color = NA)("foo")
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+---
+
+ Code
+ col_quantile(bw, 0:1, na.color = NA)(-1)
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+---
+
+ Code
+ col_quantile(bw, 0:1, na.color = NA)(2)
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+---
+
+ Code
+ col_numeric(bw, c(0, 1), na.color = NA)(-1)
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+---
+
+ Code
+ col_numeric(bw, c(0, 1), na.color = NA)(2)
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+# factors match by name, not position
+
+ Code
+ col <- pal(letters[10:20])
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+
+# qualitative palettes don't interpolate
+
+ Code
+ pal(letters[6])
+ Condition
+ Warning:
+ Some values were outside the color scale and will be treated as NA
+ Output
+ [1] NA
+
+# OK, qualitative palettes sometimes interpolate
+
+ Code
+ result <- pal(letters[1:20])
+ Condition
+ Warning in `RColorBrewer::brewer.pal()`:
+ n too large, allowed maximum for palette Accent is 8
+ Returning the palette you asked for with that many colors
+
# col_quantile handles skewed data
Code
diff --git a/tests/testthat/_snaps/label-bytes.md b/tests/testthat/_snaps/label-bytes.md
index e995d89c..66b7f49f 100644
--- a/tests/testthat/_snaps/label-bytes.md
+++ b/tests/testthat/_snaps/label-bytes.md
@@ -6,3 +6,52 @@
Error:
! `units` must be one of "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", or "YiB", not "unit".
+# Byte formatter can take a symbol designator
+
+ Code
+ number_bytes(c(50, 400, 502, NA), symbol = "k")
+ Condition
+ Warning:
+ `symbol` must be one of auto, B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, or YiB
+ i The provided value ("k") will be changed to the default ("auto")
+ Output
+ [1] "50 B" "400 B" "502 B" NA
+
+---
+
+ Code
+ number_bytes(1024^(1:2), "kB", units = "binary")
+ Condition
+ Warning:
+ `symbol` must be one of auto, B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, or YiB
+ i The provided value ("kB") will be changed to the default ("auto")
+ Output
+ [1] "1 KiB" "1 MiB"
+
+---
+
+ Code
+ number_bytes(1024^(1:2), "KiB", units = "si")
+ Condition
+ Warning:
+ `symbol` must be one of auto, B, kB, MB, GB, TB, PB, EB, ZB, or YB
+ i The provided value ("KiB") will be changed to the default ("auto")
+ Output
+ [1] "1 kB" "1 MB"
+
+# Byte formatter throws informative error for wrong length symbol
+
+ Code
+ number_bytes(symbol = character())
+ Condition
+ Error in `validate_byte_symbol()`:
+ ! `symbol` must have length 1, not length 0
+
+---
+
+ Code
+ number_bytes(symbol = c("kB", "MB"))
+ Condition
+ Error in `validate_byte_symbol()`:
+ ! `symbol` must have length 1, not length 2
+
diff --git a/tests/testthat/_snaps/pal-manual.md b/tests/testthat/_snaps/pal-manual.md
new file mode 100644
index 00000000..b88fcd82
--- /dev/null
+++ b/tests/testthat/_snaps/pal-manual.md
@@ -0,0 +1,10 @@
+# pal_manual gives warning if n greater than the number of values
+
+ Code
+ pal_manual(c("red", "blue", "green"))(4)
+ Condition
+ Warning:
+ This manual palette can handle a maximum of 3 values. You have supplied 4
+ Output
+ [1] "red" "blue" "green" NA
+
diff --git a/tests/testthat/_snaps/palette-registry.md b/tests/testthat/_snaps/palette-registry.md
new file mode 100644
index 00000000..09e679c8
--- /dev/null
+++ b/tests/testthat/_snaps/palette-registry.md
@@ -0,0 +1,16 @@
+# palette getters and setters work as intended
+
+ Code
+ get_palette("rgb")
+ Condition
+ Error in `get_palette()`:
+ ! Unknown palette: rgb
+
+---
+
+ Code
+ set_palette("foobar", list(a = 1:2, b = "A"))
+ Condition
+ Error in `set_palette()`:
+ ! The `palette` argument must be a or vector.
+
diff --git a/tests/testthat/_snaps/scale-continuous.md b/tests/testthat/_snaps/scale-continuous.md
new file mode 100644
index 00000000..a3d4dcf6
--- /dev/null
+++ b/tests/testthat/_snaps/scale-continuous.md
@@ -0,0 +1,9 @@
+# train_continuous stops on discrete values
+
+ Code
+ train_continuous(LETTERS[1:5])
+ Condition
+ Error:
+ ! Discrete value supplied to a continuous scale.
+ i Example values: "A", "B", "C", "D", and "E".
+
diff --git a/tests/testthat/_snaps/trans-compose.md b/tests/testthat/_snaps/trans-compose.md
index c2f772a1..ebd48db9 100755
--- a/tests/testthat/_snaps/trans-compose.md
+++ b/tests/testthat/_snaps/trans-compose.md
@@ -11,3 +11,11 @@
Error in `transform_compose()`:
! Sequence of transformations yields invalid domain
+# produces correct domains
+
+ Code
+ transform_compose("sqrt", "reverse", "log")$domain
+ Condition
+ Error in `transform_compose()`:
+ ! Sequence of transformations yields invalid domain
+
diff --git a/tests/testthat/_snaps/trans-numeric.md b/tests/testthat/_snaps/trans-numeric.md
new file mode 100644
index 00000000..8b138967
--- /dev/null
+++ b/tests/testthat/_snaps/trans-numeric.md
@@ -0,0 +1,18 @@
+# Boxcox gives error for negative values
+
+ Code
+ trans$trans(-10:10)
+ Condition
+ Error in `trans$trans()`:
+ ! `transform_boxcox()` must be given only positive values
+ i Consider using `transform_modulus()` instead?
+
+---
+
+ Code
+ trans$trans(-10:10)
+ Condition
+ Error in `trans$trans()`:
+ ! `transform_boxcox()` must be given only positive values
+ i Consider using `transform_modulus()` instead?
+
diff --git a/tests/testthat/_snaps/utils.md b/tests/testthat/_snaps/utils.md
new file mode 100644
index 00000000..0d5ef19a
--- /dev/null
+++ b/tests/testthat/_snaps/utils.md
@@ -0,0 +1,24 @@
+# recycle_common throws appropriate errors
+
+ Code
+ recycle_common(a = 1:2, size = 3)
+ Condition
+ Error:
+ ! Cannot recycle `a` to length 3.
+
+---
+
+ Code
+ recycle_common(a = 1:2, b = 1:3)
+ Condition
+ Error:
+ ! Cannot recycle `a` and `b` to a common size.
+
+---
+
+ Code
+ recycle_common(a = 1:2, b = 1:3, size = 3)
+ Condition
+ Error:
+ ! Cannot recycle `a` to length 3.
+
diff --git a/tests/testthat/test-colour-mapping.R b/tests/testthat/test-colour-mapping.R
index 8cfebec0..81e03c3e 100644
--- a/tests/testthat/test-colour-mapping.R
+++ b/tests/testthat/test-colour-mapping.R
@@ -19,11 +19,11 @@ test_that("Outside of domain returns na.color", {
expect_true(is.na(col_numeric(bw, c(0, 1), na.color = NA)(-1)))
expect_true(is.na(col_numeric(bw, c(0, 1), na.color = NA)(2)))
})
- expect_warning(col_factor(bw, letters, na.color = NA)("foo"))
- expect_warning(col_quantile(bw, 0:1, na.color = NA)(-1))
- expect_warning(col_quantile(bw, 0:1, na.color = NA)(2))
- expect_warning(col_numeric(bw, c(0, 1), na.color = NA)(-1))
- expect_warning(col_numeric(bw, c(0, 1), na.color = NA)(2))
+ expect_snapshot(col_factor(bw, letters, na.color = NA)("foo"))
+ expect_snapshot(col_quantile(bw, 0:1, na.color = NA)(-1))
+ expect_snapshot(col_quantile(bw, 0:1, na.color = NA)(2))
+ expect_snapshot(col_numeric(bw, c(0, 1), na.color = NA)(-1))
+ expect_snapshot(col_numeric(bw, c(0, 1), na.color = NA)(2))
})
test_that("Basic color accuracy", {
@@ -72,7 +72,7 @@ test_that("factors match by name, not position", {
expect_identical(pal(partial), pal(droplevels(partial)))
# Sending in values outside of the color scale should result in a warning and na.color
- expect_warning(col <- pal(letters[10:20]))
+ expect_snapshot(col <- pal(letters[10:20]))
expect_true(all(is.na(col)))
})
@@ -110,7 +110,7 @@ test_that("qualitative palettes don't interpolate", {
)
# Values outside of the originally provided levels should be NA with warning
- expect_warning(pal(letters[6]))
+ expect_snapshot(pal(letters[6]))
expect_true(suppressWarnings(is.na(pal(letters[6]))))
})
@@ -122,7 +122,7 @@ test_that("OK, qualitative palettes sometimes interpolate", {
name = "Accent"
)
- expect_warning(result <- pal(letters[1:20]))
+ expect_snapshot(result <- pal(letters[1:20]))
# The first and last levels are the first and last palette colors
expect_true(all(result[c(1, 20)] %in% allColors))
# All the rest are interpolated though
diff --git a/tests/testthat/test-label-bytes.R b/tests/testthat/test-label-bytes.R
index d18fab94..335c9f3f 100644
--- a/tests/testthat/test-label-bytes.R
+++ b/tests/testthat/test-label-bytes.R
@@ -40,7 +40,7 @@ test_that("Byte formatter can take a symbol designator", {
)
# informative warning for incorrect spelling
- expect_warning(number_bytes(c(50, 400, 502, NA), symbol = "k"), "must be")
+ expect_snapshot(number_bytes(c(50, 400, 502, NA), symbol = "k"))
# respects unit designation
expect_equal(number_bytes(1024, accuracy = .01), c("1.00 KiB"))
@@ -58,8 +58,8 @@ test_that("Byte formatter can take a symbol designator", {
)
# unit system is enforced
- expect_warning(number_bytes(1024^(1:2), "kB", units = "binary"), "KiB")
- expect_warning(number_bytes(1024^(1:2), "KiB", units = "si"), "kB")
+ expect_snapshot(number_bytes(1024^(1:2), "kB", units = "binary"))
+ expect_snapshot(number_bytes(1024^(1:2), "KiB", units = "si"))
})
test_that("Byte formatter handles zero values", {
@@ -80,8 +80,8 @@ test_that('Byte formatter symbol = "auto" can show variable multiples', {
})
test_that("Byte formatter throws informative error for wrong length symbol", {
- expect_error(number_bytes(symbol = character()), "not length 0")
- expect_error(number_bytes(symbol = c("kB", "MB")), "not length 2")
+ expect_snapshot(number_bytes(symbol = character()), error = TRUE)
+ expect_snapshot(number_bytes(symbol = c("kB", "MB")), error = TRUE)
})
test_that("preserves names", {
diff --git a/tests/testthat/test-pal-manual.R b/tests/testthat/test-pal-manual.R
index d77e05a4..36ca96a6 100644
--- a/tests/testthat/test-pal-manual.R
+++ b/tests/testthat/test-pal-manual.R
@@ -1,5 +1,5 @@
test_that("pal_manual gives warning if n greater than the number of values", {
- expect_warning(pal_manual(c("red", "blue", "green"))(4), "can handle a maximum")
+ expect_snapshot(pal_manual(c("red", "blue", "green"))(4))
})
test_that("pal_manual returns an unnamed vector", {
diff --git a/tests/testthat/test-palette-registry.R b/tests/testthat/test-palette-registry.R
index 06e09074..70d1dc24 100644
--- a/tests/testthat/test-palette-registry.R
+++ b/tests/testthat/test-palette-registry.R
@@ -4,13 +4,10 @@ test_that("palette getters and setters work as intended", {
expect_in(c("hue", "grey"), palette_names())
# We cannot get unknown palettes
- expect_error(get_palette("rgb"), "Unknown palette")
+ expect_snapshot(get_palette("rgb"), error = TRUE)
# We cannot set nonsense palettes
- expect_error(
- set_palette("foobar", list(a = 1:2, b = "A")),
- "must be a"
- )
+ expect_snapshot(set_palette("foobar", list(a = 1:2, b = "A")), error = TRUE)
# Test we can set custom palettes
colours <- c("red", "green", 'blue')
diff --git a/tests/testthat/test-range.R b/tests/testthat/test-range.R
index 3318b080..d7a66767 100644
--- a/tests/testthat/test-range.R
+++ b/tests/testthat/test-range.R
@@ -1,6 +1,6 @@
test_that("R6 inheritance works", {
- expect_error(ContinuousRange$new(), NA)
- expect_error(DiscreteRange$new(), NA)
+ expect_no_error(ContinuousRange$new())
+ expect_no_error(DiscreteRange$new())
expect_true(R6::is.R6(ContinuousRange$new()))
expect_true(R6::is.R6(DiscreteRange$new()))
})
diff --git a/tests/testthat/test-scale-continuous.R b/tests/testthat/test-scale-continuous.R
index 8c8e3074..0682c061 100644
--- a/tests/testthat/test-scale-continuous.R
+++ b/tests/testthat/test-scale-continuous.R
@@ -9,9 +9,7 @@ test_that("NA.value works for continuous scales", {
})
test_that("train_continuous stops on discrete values", {
- expect_error(train_continuous(LETTERS[1:5]),
- regexp = "Discrete value supplied"
- )
+ expect_snapshot(train_continuous(LETTERS[1:5]), error = TRUE)
})
test_that("train_continuous strips attributes", {
diff --git a/tests/testthat/test-trans-compose.R b/tests/testthat/test-trans-compose.R
index 06a95d9f..ac7d87e0 100644
--- a/tests/testthat/test-trans-compose.R
+++ b/tests/testthat/test-trans-compose.R
@@ -34,5 +34,5 @@ test_that("produces correct domains", {
expect_equal(transform_compose("log", "log")$domain, c(1, Inf))
expect_equal(transform_compose("reverse", "log")$domain, c(-Inf, 0))
expect_equal(transform_compose("reverse", "logit", "log")$domain, c(-1, -0.5))
- expect_error(transform_compose("sqrt", "reverse", "log")$domain, "invalid domain")
+ expect_snapshot(transform_compose("sqrt", "reverse", "log")$domain, error = TRUE)
})
diff --git a/tests/testthat/test-trans-numeric.R b/tests/testthat/test-trans-numeric.R
index ff3523bc..6782ea15 100644
--- a/tests/testthat/test-trans-numeric.R
+++ b/tests/testthat/test-trans-numeric.R
@@ -17,9 +17,9 @@ test_that("Modulus is invertible for negative and positive numbers", {
test_that("Boxcox gives error for negative values", {
trans <- transform_boxcox(p = .1)
- expect_error(trans$trans(-10:10))
+ expect_snapshot(trans$trans(-10:10), error = TRUE)
trans <- transform_boxcox(p = -2)
- expect_error(trans$trans(-10:10))
+ expect_snapshot(trans$trans(-10:10), error = TRUE)
})
test_that("Boxcox can handle NA values", {
diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R
index d4626554..1451d377 100644
--- a/tests/testthat/test-utils.R
+++ b/tests/testthat/test-utils.R
@@ -1,17 +1,5 @@
test_that("recycle_common throws appropriate errors", {
-
- expect_error(
- recycle_common(a = 1:2, size = 3),
- "length 3"
- )
- expect_error(
- recycle_common(a = 1:2, b = 1:3),
- "common size"
- )
-
- expect_error(
- recycle_common(a = 1:2, b = 1:3, size = 3),
- "Cannot recycle `a`"
- )
-
+ expect_snapshot(recycle_common(a = 1:2, size = 3), error = TRUE)
+ expect_snapshot(recycle_common(a = 1:2, b = 1:3), error = TRUE)
+ expect_snapshot(recycle_common(a = 1:2, b = 1:3, size = 3), error = TRUE)
})