Skip to content

Commit

Permalink
fill_null -> %||%
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Dec 28, 2024
1 parent 2cb4236 commit 323c74d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
48 changes: 26 additions & 22 deletions R/draw_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,43 +150,47 @@ draw_legend = function(
## Use `!exists` rather than `is.null` for title in case user specified no title
if (!exists("title", where = legend_args)) legend_args[["title"]] = by_dep

fill_null = function(name, value) {
if (is.null(legend_args[[name]])) legend_args[[name]] = value
return(legend_args)
}
legend_args = fill_null("pch", pch)
legend_args = fill_null("lty", lty)
legend_args = fill_null("col", col)
legend_args = fill_null("bty", "n")
legend_args = fill_null("horiz", FALSE)
legend_args = fill_null("xpd", NA)
legend_args[["pch"]] = legend_args[["pch"]] %||% pch
legend_args[["lty"]] = legend_args[["lty"]] %||% lty
legend_args[["col"]] = legend_args[["col"]] %||% col
legend_args[["bty"]] = legend_args[["bty"]] %||% "n"
legend_args[["horiz"]] = legend_args[["horiz"]] %||% FALSE
legend_args[["xpd"]] = legend_args[["xpd"]] %||% NA

if (!isTRUE(type %in% c("p", "ribbon", "polygon", "polypath"))) {
legend_args = fill_null("lwd", lwd)
legend_args[["lwd"]] = legend_args[["lwd"]] %||% lwd
}

if (isTRUE(type %in% c("p", "pointrange", "errorbar")) && (length(col) == 1 || length(cex) == 1)) {
legend_args = fill_null("pt.cex", cex)
legend_args[["pt.cex"]] = legend_args[["pt.cex"]] %||% cex
}

if (isTRUE(type %in% c("rect", "ribbon", "polygon", "polypath", "boxplot", "hist", "histogram", "spineplot", "ridge")) || isTRUE(gradient)) {
legend_args[["pch"]] = 22
legend_args = fill_null("pt.cex", 3.5)
legend_args = fill_null("y.intersp", 1.25)
legend_args = fill_null("seg.len", 1.25)
legend_args[["pt.cex"]] = legend_args[["pt.cex"]] %||% 3.5
legend_args[["y.intersp"]] = legend_args[["y.intersp"]] %||% 1.25
legend_args[["seg.len"]] = legend_args[["seg.len"]] %||% 1.25
}

if (isTRUE(type %in% c("ribbon", "hist", "histogram", "spineplot"))) {
legend_args = fill_null("pt.lwd", 0)
legend_args[["pt.lwd"]] = legend_args[["pt.lwd"]] %||% 0
}

if (identical(type, "n") && isFALSE(gradient)) {
legend_args = fill_null("pch", par("pch"))
legend_args[["pch"]] = legend_args[["pch"]] %||% par("pch")
}

if (identical(type, "spineplot")) {
legend_args = fill_null("pt.bg", legend_args[["col"]])
legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% legend_args[["col"]]
}

if (identical(type, "ridge") && isFALSE(gradient)) {
legend_args = fill_null("pt.bg", sapply(legend_args[["col"]], function(ccol) seq_palette(ccol, n = 2)[2]))
}
legend_args = fill_null("pt.bg", bg)
legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% sapply(legend_args[["col"]], function(ccol) seq_palette(ccol, n = 2)[2])
}

legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% bg

legend_args = fill_null("legend", lgnd_labs)
legend_args[["legend"]] = legend_args[["legend"]] %||% lgnd_labs
if (length(lgnd_labs) != length(eval(legend_args[["legend"]]))) {
warning(
"\nUser-supplied legend labels do not match the number of groups.\n",
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ more_than_n_unique = function(x, n, small_vec_len = 1e3L) {
return(FALSE)
}
}


if (getRversion() <= "4.4.0") {
`%||%` = function(x, y) if (is.null(x)) y else x
}

0 comments on commit 323c74d

Please sign in to comment.