How to import operators from the patchwork package into my own package in R?

35 Views Asked by At

Similar questions have been asked here, here, and here. However, they don't exactly solve the issue Im having.

Essentially, I am creating a package that has a function that creates a bar plot and then attaches other plots to the y-axis (a simple version of which is shown in the code below).

library(ggplot2)

df <- data.frame(vals = c(10, 5, 18),
                 name = c("A", "B", "C"))
bp <- df |> 
  ggplot() +
  geom_bar(aes(x = name, y = vals), stat = "identity") +
  coord_flip() +
  xlab("") +
  theme_bw() +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) 

# create plots to use as x-axis --------------------------------------------

p1 <- ggplot(df, aes(x = vals, y = vals)) + geom_point() + theme_bw() +
  theme(axis.title.x = element_blank(),
        axis.text.x  = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y  = element_blank(),
        axis.ticks.y = element_blank()) 

p3 <- p2 <- p1

# turn into list of plots
myList <- list(p1, p2, p3)

# -------------------------------------------------------------------------

# attach plots to axis

width <- .9 # Default width of bars
p_axis <- ggplot(df) +
  geom_blank(aes(y = name)) +
  purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), ymin = .y - width / 2, ymax = .y + width / 2)) +
  theme_void() 

library(patchwork)
p_axis | bp

In the last line I am using the | operator from patchwork package. However, I'm not sure how I can explicitly import that operator into my own package. In my roxygen documentation, I tried @importFrom patchwork "%|%"... but when I run devtools:document() it says:

Warning message: object ‘|’ is not exported by 'namespace:patchwork'

I have tried various combinations like @importFrom patchwork "|", @importFrom patchwork %|%, etc., but all yield the same result.

Investigating it further it seems like the | operator may be an internal function in patchwork. For example, you can find it via ?patchwork:::|.ggplot. So, since it's using the syntax patchwork:::, that means it's not exported, right!?

So is there a way to import that operator or is my only option to just import the full package (i.e., @import patchwork).

FYI: I have tried other methods that dont use the patchwork package, but my actual function is a bit more complicated and patchwork is the only one that seems to work well for me, so I kind of have to use it.

0

There are 0 best solutions below