[Rd] Depends: R (>= 4.1) for packages that use |> and \(...)

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Thu Jan 16 12:41:21 CET 2025


Hello R-devel,

Approximately [*] the following CRAN packages make use of the pipe
syntax in their source code or examples without depending on R >= 4.1:

 [1] "biplotEZ"           "CaseBasedReasoning" "collinear"
 [4] "cubble"             "disk.frame"         "duckdbfs"
 [7] "eia"                "feltr"              "flattabler"
[10] "geodimension"       "hgnc"               "himach"
[13] "lay"                "lidR"               "locateip"
[16] "particles"          "photosynthesis"     "pivotea"
[19] "planr"              "rtrek"              "satres"
[22] "sdtmval"            "selenider"          "sewage"
[25] "stminsights"        "tabr"               "tidygraph"
[28] "tidywikidatar"      "USgas"              "washi"
[31] "zctaCrosswalk"

Since we have checks in place to automatically set Depends: R (>=
2.10.0) for data files compressed with xz or bzip2 and >= 3.5.0 for
data files serialized with format version 3, would it make sense to
automatically add Depends: R (>= 4.1) for such packages?

The patch at the end of this message adds the R version dependency
during R CMD build:

R-devel CMD build --no-build-vignettes .
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘biplotEZ’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking vignette meta-information ... OK
* checking for LF line-endings in source and make files and shell
scripts
* checking for empty or unneeded directories
  NB: this package now depends on R (>= 4.1.0)
  WARNING: Added dependency on R >= 4.1.0 because some of the source
  files use the new syntax constructs.
Files making use of R >= 4.1 pipe |> or function shorthand \(...):
  biplotEZ/R/biplot.R biplotEZ/R/translate_axes.R
* building ‘biplotEZ_2.2.tar.gz’

A more extensive test could also look at the tests, demos, and
\examples{}, but that may take longer and open the door for false
positives. A package that uses |> in one example would still be useful
on R-4.0.0, while a package that uses |> in the source code would fail
to parse and install.

Index: src/library/tools/R/QC.R
===================================================================
--- src/library/tools/R/QC.R	(revision 87545)
+++ src/library/tools/R/QC.R	(working copy)
@@ -10367,7 +10367,29 @@
     }
 }
 
+.check_use_of_R41_syntax <-
+function(files)
+{
+    out <- vapply(files,
+           function(f) tryCatch(
+               any(getParseData(parse(f, keep.source = TRUE))$token %in% c("PIPE", "'\\\\'")),
+               error = function(e) FALSE
+           ),
+           FALSE)
+    out <- files[out]
+    class(out) <- "check_use_of_R41_syntax"
+    out
+}
 
+format.check_use_of_R41_syntax <-
+function(x, ...)
+{
+    if (length(x)) {
+        c("Files making use of R >= 4.1 pipe |> or function shorthand \\(...):",
+          .strwrap22(x, " "))
+    } else character()
+}
+
 ### Local variables: ***
 ### mode: outline-minor ***
 ### outline-regexp: "### [*]+" ***
Index: src/library/tools/R/build.R
===================================================================
--- src/library/tools/R/build.R	(revision 87545)
+++ src/library/tools/R/build.R	(working copy)
@@ -1165,9 +1165,11 @@
         desc <- .read_description(file.path(pkgname, "DESCRIPTION"))
         Rdeps <- .split_description(desc)$Rdepends2
         hasDep350 <- FALSE
+        hasDep410 <- FALSE
         for(dep in Rdeps) {
             if(dep$op != '>=') next
             if(dep$version >= "3.5.0") hasDep350 <- TRUE
+            if(dep$version >= "4.1.0") hasDep410 <- TRUE
         }
         if (!hasDep350) {
             ## re-read files after exclusions have been applied
@@ -1189,6 +1191,23 @@
                          "\n")
             }
         }
+        if (!hasDep410) {
+            uses410 <- .check_use_of_R41_syntax(dir(file.path(pkgname, "R"),
+                                                    full.names = TRUE,
+                                                    pattern = "[.]R$",
+                                                    ignore.case = TRUE))
+            if (length(uses410)) {
+                fixup_R_dep(pkgname, "4.1.0")
+                msg <- paste("WARNING: Added dependency on R >= 4.1.0 because",
+                             "some of the source files use the new syntax",
+                             "constructs.")
+                printLog(Log,
+                         paste(c(strwrap(msg, indent = 2L, exdent = 2L),
+                                 format(uses410)),
+                               collapse = "\n"),
+                               "\n")
+            }
+        }
 
 	## add NAMESPACE if the author didn't write one
 	if(!file.exists(namespace <- file.path(pkgname, "NAMESPACE")) ) {


-- 
Best regards,
Ivan

[*] Based on the following GitHub search, which requires logging in:
https://github.com/search?q=org%3Acran%20path%3A%2F%5B.%5D%5BRr%5Dd%3F%24%2F%20%2F%5Cs%5C%7C%3E%2F&type=code
There's currently no REST API support for regexp search, so the list
was obtained manually.



More information about the R-devel mailing list