Packages with different R Versions

Is it common/acceptable to use getRversion() in packages and to only load certain functions depending on the version of R the user has.

I am writing a package that uses the deparse1() and str2lang() functions. deparse1() is from R 4.0.0 and str2lang() is from R 3.6.0. I have included a bit of code to include these functions if the user is using an older version of R:

if(getRversion() < "4.0") {
deparse1 <- function(expr, collapse = " ", width.cutoff = 500L, ...)
  paste(deparse(expr, width.cutoff, ...), collapse = collapse)

if(getRversion() < "3.6"){
  str2lang <- function(s) parse(text = s, keep.source=FALSE)[[1]]

The definition for deparse1() is exact, however str2lang() uses an Internal function, but the documentation says it does roughly this (with a few additional checks)

It has passed check() on my system, so including these lines of code (which don't run) is not "illegal" as far as R CMD CHECK is concerned.

Has anyone seen this kind of thing done elsewhere? I know that some packages do this inside of functions, but this is not within a function, and is just in my R/ folder

Yes, I saw this in ggplot2 in .onLoad()

Do you know about the backports package, by the way?

1 Like

I think that the ggplot2 solution is used to remove the pathId.lengths argument from being processed by the pathGrob() function. But it does seem like a feasible solution.

I have heard of backports, but I hadn't read the docs for a while. Just had a look and it recommends you can import a single function from backports if you just need the one instead of loading up the entire thing, so this might be the solution I follow.

1 Like

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.