sam81
July 19, 2018, 10:54am
1
I'm trying to create a custom ggplot2 geom by modifying geom_errorbar
. The geom_errorbar
function uses an operator that I've never seen before %||%
, on line 37 here:
#' @export
#' @rdname geom_linerange
geom_errorbar <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomErrorbar,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
This file has been truncated. show original
does anyone know what it is, and which package define it? Trying to run my custom geom I get the error:
Error in data$width %||% params$width %||% (resolution(data$x, FALSE) * (from geom_errorbar2.R#40) :
could not find function "%||%"
1 Like
mara
July 19, 2018, 11:14am
2
%||%
is an operator from rlang to that lets you replace the default value for NULL.
From rlang docs :
This infix function makes it easy to replace NULLs with a default value. It's inspired by the way that Ruby's or operation (||) works.
It takes arguments x
and y
, where if x
is NULL
, will return y
; otherwise returns x
.
x %||% y
The reason your code doesn't work (I assume, from your snippet above) is because you need to import the function %||%
in order to use it.
You can see the Specifying imports and exports section of Writing R Extensions , or the Namespace section of the R Packages book for more detail.
Learn how to create a package, the fundamental unit of shareable, reusable, and reproducible R code.
5 Likes
sam81
July 19, 2018, 11:27am
3
thank you very much for the very informative answer! I'm able to run my custom geom by loading the rlang
package.
2 Likes