Re: [Rd] Warning on backslash sequences (was sprintf behavior)

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 27 Sep 2006 - 09:10:49 GMT

On Tue, 26 Sep 2006, Bill Dunlap wrote:

> On Mon, 25 Sep 2006 ripley@stats.ox.ac.uk wrote:
>
>> On Mon, 25 Sep 2006, michael.bauer@lo-res.org wrote:
>> ...
>>> sprintf("\p") doesn't show the backslash, this occurs with all strings that
>>> start with certain letters. There is however no explanation to this behavior.
>>
>> There is: see ?Quotes (and C behaves in the same way).
>>
>>> And there seems to be no way to get a guaranteed backslash in sprintf.
>>
>> "\\", see the FAQ 7.8 for example.
>
> Splus's parser emits a warning when it sees a backslash
> outside of the recognized backslash sequence. E.g.,
>
> > nchar("\Backslashed?")
> [1] 12
> Warning messages:
> The initial backslash is ignored in \B -- not a recognized escape sequence.
> Use \\ to make a backslash
>
> You might want to add that warning to R's parser. I've
> seen the error in several R packages. E.g.,
>

> bayesmix/R/JAGScontrol.R: text[4] <- "-inits.R\"\n\initialize\n"
> SciViews/svDialogs/R/fixedDlg.wxPython.R: if (length(grep("[\.]", basename(res))) == 0)

>
> The warning is mostly emitted when the error is benign, but it
> might help get people to think about what they are typing.

I am not at all sure about this. R's documentation says

      Backslash is used to start an escape sequence inside character
      constants. Unless specified in the following table, an escaped
      character is interpreted as the character itself. Single quotes
      need to be escaped by backslash in single-quoted strings, and
      double quotes in double-quoted strings.

        '\n'          newline
        '\r'          carriage return
        '\t'          tab
        '\b'          backspace
        '\a'          alert (bell)
        '\f'          form feed
        '\v'          vertical tab
        '\\'          backslash '\'
        '\nnn'        character with given octal code (1, 2 or 3 digits)
        '\xnn'        character with given hex code (1 or 2 hex digits)
        '\unnnn'      Unicode character with given code (1-4 hex digits)
        '\Unnnnnnnn'  Unicode character with given code (1-8 hex digits)

so it is not an error in R. People tend not to like being warned about legitimate usage (and one can see this sort of thing being intentional in machine-generated scripts: for example to escape spaces in file paths and to escape line feeds).

What exactly does Splus's parser allow as intentional?

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Sep 27 19:12:02 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 27 Sep 2006 - 15:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.