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

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Sep 27 11:10:49 CEST 2006

On Tue, 26 Sep 2006, Bill Dunlap wrote:

> On Mon, 25 Sep 2006 ripley at stats.ox.ac.uk wrote:
>> On Mon, 25 Sep 2006, michael.bauer at 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 at 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

More information about the R-devel mailing list