Handle multiple package dependency versions without triggering R code check NOTE

Hi all,

I am maintainer of an R package on CRAN which depends on ggplot2 and includes a ggplot2 theme. The in the new version of ggplot2 coming later this months there is a breaking change in the argument size will be replaced by linewidth (blog post here) in my case the change affects element_line() in the theme.

I need to maintain compatibility with the recent ggplot2 version so I have successfully handled this breaking change with utils::packageVersion as shown the code below. However when building my package or running a CRAN check with ggplot2 <= 3.3.6. I get the following NOTE:

checking R code for possible problems ... NOTE
Note: possible error in 'element_line(linewidth = 0.25)", ': unused argument (linewidth = 0.25) at themes_gg.R:33

Because of this all my continuous integration workflows fail.

So my question is: is there a way to prevent from R code that is not supposed to be run under the current version? Or is there a better approach than the one I have used to handle this?


R code example from handling code across multiple dependency versions:

if (utils::packageVersion("ggplot2") > "3.3.6") {
    panel_line <- element_line(linewidth = 0.25)
  } else {
    panel_line <- element_line(size = 0.25)
  }
  
theme(panel.grid.major = panel_line)
1 Like

This does not seem like a great solution, but I don't know anything better. You can create a wrapper function to element_line() which calls it like this, using asNamespace():

element_line <- function(colour = NULL, linewidth = NULL, ...) {
  if (utils::packageVersion("ggplot2") > "3.3.6") {
    asNamespace("ggplot2")$element_line(colour = colour, linewidth = linewidth, ...)
  } else {
    asNamespace("ggplot2")$element_line(colour = colour, size = linewidth, ...)
  }
}

I am pretty sure that R CMD check will not warn for this.

1 Like

@Gabor thanks a lot for your suggestion it worked like a charm!! I had started to consider a do.call() type of approach to "hide" the code from R CMD check but your suggestion is better in my case.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.