Rainer M Krug
2015-Apr-16 10:19 UTC
[R] "function" as variable name - probably better error message
Hi The following code works as expected: list(plot="Not any more!") ,---- | > plot <- "Not any more!") | [1] "Not any more!" `---- But for this I get an error: ,---- | > function <- "Not any more!" | Error: unexpected assignment in "function <-" `---- The error message is quite cryptic and does not help much further. Would it be possible to provide a more useful error message in this case that (presumably) "function" is a reserved word? Along the same lines - is there a list of reserved words which can not be used in R as variable names (not even as elements in a a list())? This is not a huge problem, but it cost me a few minutes of figuring out. Thanks, Rainer -- Rainer M. Krug email: Rainer<at>krugs<dot>de PGP: 0x0F52F982 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 494 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150416/a23c0afc/attachment.bin>
Duncan Murdoch
2015-Apr-16 11:38 UTC
[R] "function" as variable name - probably better error message
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 On 16/04/2015 6:19 AM, Rainer M Krug wrote:> Hi > > The following code works as expected: > > > list(plot="Not any more!") > > ,---- | > plot <- "Not any more!") | [1] "Not any more!" `---- > > But for this I get an error: > > ,---- | > function <- "Not any more!" | Error: unexpected > assignment in "function <-" `---- > > The error message is quite cryptic and does not help much further. > Would it be possible to provide a more useful error message in this > case that (presumably) "function" is a reserved word? > > Along the same lines - is there a list of reserved words which can > not be used in R as variable names (not even as elements in a a > list())?The R Language Definition lists the reserved words (see section 10.3.3). It oversimplifies things, saying they can't be used as variable names, when in fact almost any string can be used with proper quoting. For example, both of these work: "function" <- "Not any more!" `function` <- "Not any more!" Duncan Murdoch> > This is not a huge problem, but it cost me a few minutes of > figuring out. > > Thanks, > > Rainer > > > > ______________________________________________ R-help at r-project.org > mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the > posting guide http://www.R-project.org/posting-guide.html and > provide commented, minimal, self-contained, reproducible code. >-----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) Comment: GPGTools - https://gpgtools.org iQEcBAEBCgAGBQJVL58yAAoJEHE2Kz23YMZyyv0H/jKjwwkQ1v7jIRsZ9hxcJECU HHHuAEusv+pgJTybdxn66XykCXQOgaLmGPGksK+CcJrBgLW2WgAh4C0CUpN36DPn TOYpos9i3wZ8k0Idr8xiEeQ/PcD0pkMEwd6oYY0lL2Ikribu8maVKxcoYvjNIJOE 3E3qrrpZfH9U2a9ws9jxJk8O3n+OQ7x8GQqUNTQHthd5McHZ63F7uIEBH/tHT7Tn x3gXAFbuSBJ6TtqaOZdPYFh3tV7CNyvJOso8yreSWK+0ZksXGKBtqElDJM0atrAj bOwEAJe6gGrEPx92NUWkTWeILDa6v96WZzprwLDi+cwt7jItILFligaXlookbf0=vKWN -----END PGP SIGNATURE-----
peter dalgaard
2015-Apr-16 14:58 UTC
[R] "function" as variable name - probably better error message
Actually, to split a few hairs, I think the documentation is essentially correct. Reserved words are not the same as quoted strings. You cannot use them in syntactically the same way that you usually use variable names, as unquoted strings in expressions. They can BE variable names, or more precisely: object names. In fact they often are; you get in deep trouble if you redefine `for`, `function`, or `if` -- as functions, at least. That goes for things like `{` too, by the way. -pd On 16 Apr 2015, at 13:38 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA512 > > On 16/04/2015 6:19 AM, Rainer M Krug wrote: >> Hi >> >> The following code works as expected: >> >> >> list(plot="Not any more!") >> >> ,---- | > plot <- "Not any more!") | [1] "Not any more!" `---- >> >> But for this I get an error: >> >> ,---- | > function <- "Not any more!" | Error: unexpected >> assignment in "function <-" `---- >> >> The error message is quite cryptic and does not help much further. >> Would it be possible to provide a more useful error message in this >> case that (presumably) "function" is a reserved word? >> >> Along the same lines - is there a list of reserved words which can >> not be used in R as variable names (not even as elements in a a >> list())? > > The R Language Definition lists the reserved words (see section > 10.3.3). It oversimplifies things, saying they can't be used as > variable names, when in fact almost any string can be used with proper > quoting. For example, both of these work: > > "function" <- "Not any more!" > `function` <- "Not any more!" > > Duncan Murdoch > >> >> This is not a huge problem, but it cost me a few minutes of >> figuring out. >> >> Thanks, >> >> Rainer >> >> >> >> ______________________________________________ R-help at r-project.org >> mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the >> posting guide http://www.R-project.org/posting-guide.html and >> provide commented, minimal, self-contained, reproducible code. >> > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG/MacGPG2 v2.0.22 (Darwin) > Comment: GPGTools - https://gpgtools.org > > iQEcBAEBCgAGBQJVL58yAAoJEHE2Kz23YMZyyv0H/jKjwwkQ1v7jIRsZ9hxcJECU > HHHuAEusv+pgJTybdxn66XykCXQOgaLmGPGksK+CcJrBgLW2WgAh4C0CUpN36DPn > TOYpos9i3wZ8k0Idr8xiEeQ/PcD0pkMEwd6oYY0lL2Ikribu8maVKxcoYvjNIJOE > 3E3qrrpZfH9U2a9ws9jxJk8O3n+OQ7x8GQqUNTQHthd5McHZ63F7uIEBH/tHT7Tn > x3gXAFbuSBJ6TtqaOZdPYFh3tV7CNyvJOso8yreSWK+0ZksXGKBtqElDJM0atrAj > bOwEAJe6gGrEPx92NUWkTWeILDa6v96WZzprwLDi+cwt7jItILFligaXlookbf0> =vKWN > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com