From pdalgd at gmail.com  Thu Jun 30 23:48:12 2011
From: pdalgd at gmail.com (peter dalgaard)
Date: Thu, 30 Jun 2011 23:48:12 +0200
Subject: [Rd] Syntactically valid names
In-Reply-To: <BANLkTimg=TM0hYA1=Obknb+486Hv8T7Fdw@mail.gmail.com>
References: <BANLkTimg=TM0hYA1=Obknb+486Hv8T7Fdw@mail.gmail.com>
Message-ID: <2698F67A-DDF9-4EAF-B7A8-A4FAAE9CCC6B@gmail.com>


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



[Rd] Syntactically valid names
peter dalgaard