[Rd] Syntactically valid names

peter dalgaard pdalgd at gmail.com
Thu Jun 30 23:48:12 CEST 2011


On Jun 30, 2011, at 22:37 , Hadley Wickham wrote:

> Hi all,
> 
> Is there any easy way to tell if a string is a syntactically valid name?
> 
> e.g.
> 
> is.syntactic("X123")
> # TRUE
> is.syntactic("[[")
> # FALSE
> 
> One implementation would be:
> 
> is.syntactic <- function(x) x == make.names(x)
> 
> but I wonder if there's a more elegant way.

I wouldn't expect so. The basic structure might be handled using a regexp of sorts, but even that is tricky because of the "dot not followed by number" rule, and then there's the stop list of reserved words, which would make your code clumsy whatever you do.

How on Earth would you expect anything to be significantly more elegant than your

function(x) x == make.names(x) 

anyway??! (OK, if there was a wrapper for the C level isValidName() function...)

-- 
Peter Dalgaard
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