From kry|ov@r00t @end|ng |rom gm@||@com  Fri Jun  2 19:58:54 2023
From: kry|ov@r00t @end|ng |rom gm@||@com (Ivan Krylov)
Date: Fri, 2 Jun 2023 20:58:54 +0300
Subject: [Rd] bug in utils:::format.person
In-Reply-To: <CAJuCY5zbzYooG+MX-b2-4Lm-=znQd4xiBe6Wh=akM7huP9AMxw@mail.gmail.com>
References: <CAJuCY5zbzYooG+MX-b2-4Lm-=znQd4xiBe6Wh=akM7huP9AMxw@mail.gmail.com>
Message-ID: <20230602205854.31df81b3@Tarkus>

On Fri, 2 Jun 2023 16:55:59 +0200
Thierry Onkelinx via R-devel <r-devel at r-project.org> wrote:

> I think I found a bug in utils::format.person when using style = "R"
> with a vector of comments. The comment section is not parsed
> properly.

Good catch! This looks like another occasion of deparse() suddenly
returning a multi-element character vector where the caller expects a
single string:

format(person(c('J.', 'Random'), 'Hacker', comment = c(ORCID =
'0000-0000-0000-0000', foo = 'bar bar bar bar bar')), style = 'R')
# [1] "person(given = c(\"J.\", \"Random\"),"
# [2] "       family = \"Hacker\","
# [3] "       comment = c(ORCID = \"0000-0000-0000-0000\", foo = \"bar
# bar bar bar bar\"))"
format(person(c('J.', 'Random'), 'Hacker', comment = c(ORCID =
'0000-0000-0000-0000', foo = 'bar bar bar bar bar bar')), style = 'R')
# [1] "person(given = c(\"J.\", \"Random\"),"
# [2] "       family = \"Hacker\","
# [3] "       comment = c(\"c(ORCID = \\\"0000-0000-0000-0000\\\", foo =
# \\\"bar bar bar bar bar bar\\\"\", \")\"))"

The following seems to fix it:

--- src/library/utils/R/citation.R	(revision 84486)
+++ src/library/utils/R/citation.R	(working copy)
@@ -1014,7 +1014,7 @@
                 function(e) {
                     e <- e[!vapply(e, is.null, NA)]
                     cargs <-
-                        sprintf("%s = %s", names(e), sapply(e, deparse))
+                        sprintf("%s = %s", names(e), sapply(e, deparse1))
                     .format_call_RR("person", cargs)
                 })
     if(length(s) > 1L)

A regression test could be along the lines of:

p <- person(
 'foo', 'bar', comment = c(
  comment = 'just enough to deparse into multiple lines',
  needs = 'multiple entries'
 )
)
stopifnot(all.equal(
 eval(parse(text = format(p, style = 'R')))$comment,
 p$comment
))

-- 
Best regards,
Ivan



[Rd] bug in utils:::format.person
Ivan Krylov