[Rd] Unclosed parenthesis in grep.Rd

Duncan Murdoch murdoch.duncan at gmail.com
Mon Mar 5 20:50:41 CET 2018


On 05/03/2018 11:48 AM, Etienne Sanchez wrote:
> There are probably more unmatched parentheses around:
> 
> detect <- function(file) {
>    text <- paste(readLines(file), collapse = "")
>    nchar(gsub("[^(]", "", text)) != nchar(gsub("[^)]", "", text))
> }
> 
> docs <- list.files("r-source-trunk/src/library",
>                     pattern = "\\.Rd$",
>                     full.names = TRUE,
>                     recursive = TRUE)
> 
> suspicious <- docs[sapply(docs, detect)]
> 
> length(suspicious)
> # [1] 114

Doing an automatic search is a good idea.  Here's a function that finds 
some errors that would be missed by yours, and gives a more informative 
report.

detect <- function(file) {
   text <- readLines(file)
   letters <- strsplit(text, "")
   line <- unlist(lapply(seq_along(letters), function(i) rep(i, 
length(letters[[i]]))))
   column <- unlist(lapply(seq_along(letters), function(i) 
seq_len(length(letters[[i]]))))
   letters <- unlist(letters)
   open <- letters == "("
   close <- letters == ")"
   sum <- cumsum(open) - cumsum(close)

   result <- FALSE
   report <- function(msg, where) {
   	message(msg, paste(file, line[where], column[where], sep=":"))
   	message(text[line[where]])
   	message(paste(c(rep(" ", column[where] - 1), "^"), collapse = ""))
   	## rstudioapi::navigateToFile(file, line[where], column[where])
   }
   if (any(sum < 0)) {
   	report("Extra close paren: ", match(TRUE, sum < 0))
   	result <- TRUE
   }
   if (sum[length(sum)] > 0) {
   	report("Extra open paren: ", length(sum) - match(TRUE, rev(sum == 
0)) + 2)
   	result <- TRUE
   }
}

If you use RStudio, you can uncomment the ## line to have it jump to the 
location of the suspicious entry.

Duncan Murdoch

> 
> 
> 2018-03-05 9:27 GMT+01:00 Martin Maechler <maechler at stat.math.ethz.ch>:
>>
>>>>>>> Hugh Parsonage <hugh.parsonage at gmail.com>
>>>>>>>      on Mon, 5 Mar 2018 13:39:24 +1100 writes:
>>
>>      > Lines 129-131: \code{grep(value = FALSE)} returns a vector
>>      > of the indices of the elements of \code{x} that yielded a
>>      > match (or not, for \code{invert = TRUE}. This will be an
>>      > integer vector unless the input
>>
>>      > There should be a closing parenthesis after \code{invert =
>>      > TRUE}
>>
>> Thank you, Hugh!  I've added the ')' now.
>> Martin
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list