[Rd] formals() adds 0 to complex function arguments

peter dalgaard pdalgd at gmail.com
Sun Jan 19 22:16:16 CET 2014


It's not formals() that is doing you in. Rather, it is a conspiration between two things: 
(a) R always displays complex constants as x+yi, even if x is zero and (b) there really is no way to specify complex constants with non-zero  real part, i.e. 1+2i is a sum of a real and and imaginary complex constant. You can see the effect already at

> quote(1+2i)
1 + (0+2i)


> q <- quote(1+2i)
> q[[1]]
`+`
> q[[2]]
[1] 1
> q[[3]]
[1] 0+2i
> str(q)
 language 1 + (0+2i)
> str(q[[3]])
 cplx 0+2i

Someone might want to fix this by implementing a full syntax for complex constants, but meanwhile, I think a passable workaround could be

> formals(test)$a <- 1+2i
> args(test)
function (a = 1+2i) 
NULL
> test
function (a = 1+2i) 
{
}


Or maybe, less sneaky

Cplx_1plus2i <- 1+2i
test <- function(a = Cplx_1plus2i){}

-pd 


On 19 Jan 2014, at 20:45 , baptiste auguie <baptiste.auguie at gmail.com> wrote:

> Dear list,
> 
> I'm facing an issue with the automated documentation of a function using
> roxygen2. The function has a complex-valued default argument, which is
> picked up by roxygen2 using formals() to generate the corresponding Usage
> section of the Rd file. Unfortunately, it appears that formals() reformats
> complex numbers. Consider the example below,
> 
> test <- function(a = 1+2i){}
> 
>> args(test)
> function (a = 1 + (0+2i))
>  NULL
>> formals(test)
> $a
> 1 + (0+2i)
> 
> As a result, R CMD check issues a warning that my Rd file is inconsistent
> between Code and Docs. Redefining the function with this syntax (a = 1 +
> (0+2i)) doesn't help, as a new 0 gets added to become 1 + (0 + (0+2i)).
> 
> Is this reformatting necessary?
> 
> Regards,
> 
> baptiste
> 
> sessionInfo()
> R Under development (unstable) (2013-11-25 r64299)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
> 
> locale:
> [1] en_NZ.UTF-8/en_NZ.UTF-8/en_NZ.UTF-8/C/en_NZ.UTF-8/en_NZ.UTF-8
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] tools_3.1.0
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-devel mailing list