On Thu, 23 Jan 2025 11:16:48 +0100
Kurt Hornik <kurt.hor...@wu.ac.at> wrote:

> My guess would be that the new syntax is particularly prominently used
> in examples: if so, it would be good to also have coverage for this.

In today's CRAN snapshot, there turned out to be 198 packages that use
4.1 syntax in examples but not in code, 5 packages that use 4.2 syntax
in examples but 4.1 in the code, and 3 packages that use 4.2 syntax in
examples but not the code. This may be slightly imprecise because I
don't have some of the Rd macro packages installed and run
Rd2ex(stages=NULL) on manually-parsed Rd files without installing the
packages.

Attaching a patch that checks the syntax used in Rd examples at the
same time as the main R code, not necessarily the best way to perform
this check. Is it perhaps worth separating R/* checks from man/*.Rd
checks? Should R CMD check try to reuse the Rd database from the
installed copy of the package?

-- 
Best regards,
Ivan
Index: src/library/tools/R/utils.R
===================================================================
--- src/library/tools/R/utils.R	(revision 87694)
+++ src/library/tools/R/utils.R	(working copy)
@@ -2103,6 +2103,38 @@
     out
 }
 
+### ** .source_file_using_R_4.x_syntax
+
+.source_file_using_R_4.x_syntax <- function(f)
+{
+    x <- utils::getParseData(parse(f, keep.source = TRUE))
+    i1 <- which(x$token %in% c("PIPE", "'\\\\'"))
+    i2 <- which(x$token == "PLACEHOLDER")
+    if(length(i1) || length(i2)) {
+        xi <- x$id
+        xp <- x$parent
+        n1 <- rep_len("4.1.0", length(i1))
+        ## Detect experimental placeholder feature as the head of a
+        ## chain of extractions by looking at the first child of the
+        ## grandparent of the placeholder: if it is the placeholder
+        ## expression then we have the 4.3.0 syntax.
+        n2 <- ifelse(vapply(i2,
+                            function(j) {
+                                u <- xp[j]
+                                v <- xp[xi %in% u]
+                                min(xi[xp %in% v]) == u
+                            },
+                            NA),
+                     "4.3.0",
+                     "4.2.0")
+        i <- c(i1, i2)
+        data.frame(token = x$token[i],
+                   needs = c(n1, n2),
+                   text = utils::getParseText(x, xp[i]))
+    } else
+        NULL
+}
+
 ### ** .package_code_using_R_4.x_syntax
 
 .package_code_using_R_4.x_syntax <-
@@ -2109,43 +2141,31 @@
 function(dir)
 {
     dir <- file_path_as_absolute(dir)
-    wrk <- function(f) {
-        p <- file.path(dir, "R", f)
-        x <- utils::getParseData(parse(p, keep.source = TRUE))
-        i1 <- which(x$token %in% c("PIPE", "'\\\\'"))
-        i2 <- which(x$token == "PLACEHOLDER")
-        if(length(i1) || length(i2)) {
-            xi <- x$id
-            xp <- x$parent
-            n1 <- rep_len("4.1.0", length(i1))
-            ## Detect experimental placeholder feature as the head of a
-            ## chain of extractions by looking at the first child of the
-            ## grandparent of the placeholder: if it is the placeholder
-            ## expression then we have the 4.3.0 syntax.
-            n2 <- ifelse(vapply(i2,
-                                function(j) {
-                                    u <- xp[j]
-                                    v <- xp[xi %in% u]
-                                    min(xi[xp %in% v]) == u
-                                },
-                                NA),
-                         "4.3.0",
-                         "4.2.0")
-            i <- c(i1, i2)
-            data.frame(token = x$token[i],
-                       needs = c(n1, n2),
-                       text = utils::getParseText(x, xp[i]),
-                       file = rep_len(f, length(i)))
-        } else
-            NULL
+    wrk.R <- function(f)
+    {
+        ret <- .source_file_using_R_4.x_syntax(file.path(dir, "R", f))
+        if (!is.null(ret)) cbind(ret, file = f)
     }
-    one <- function(f)
-        tryCatch(wrk(f), error = function(e) NULL)
-
-    files <- list_files_with_type(file.path(dir, "R"), "code",
+    one.R <- function(f)
+        tryCatch(wrk.R(f), error = function(e) NULL)
+    files.R <- list_files_with_type(file.path(dir, "R"), "code",
                                   full.names = FALSE,
                                   OS_subdirs = c("unix", "windows"))
-    do.call(rbind, lapply(files, one))
+
+    db <- Rd_db(dir = dir)
+    wrk.Rd <- function(Rd, f)
+    {
+        exfile <- tempfile()
+        on.exit(unlink(exfile))
+        Rd2ex(Rd, exfile)
+        ret <- .source_file_using_R_4.x_syntax(exfile)
+        if (!is.null(ret)) cbind(ret, file = f)
+    }
+    one.Rd <- function(Rd, f)
+        tryCatch(wrk.Rd(Rd, f), error = function(e) NULL)
+
+    do.call(rbind, c(lapply(files.R, one.R),
+                     Map(one.Rd, db, names(db))))
 }
 
 ## ** .package_depends_on_R_at_least
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to