Skip to content

Interpretation of escaped characters in \examples{}

3 messages · Kurt Hornik, Gordon K Smyth

#
Dear Kurt,
At 03:21 PM 26/05/2003, Kurt Hornik wrote:
No I'm the one who was missed something. As the strsplit example shows, I 
could have got "*\\.txt" as I wanted in the help file by putting four 
backlashes, "*\\\\.txt", in the .Rd file.

There are still aspects though to the substitution of backlashes when going 
from the help file to running examples which seem unexpected to me. I tried 
out the following example.

In the example in the .Rd file:

c("\\\\","\\\","\\\\#","\\#","\#")
c("\\+","\\?","\\.","\\*","\\^","\\$","\\(","\\)","\\[","\\]","\\{","\\}","\\|","\\_")
c("\+","\?","\.","\*","\^","\$","\(","\)","\[","\]","\{","\}","\|","\_")
c("\\w","\\W","\\s","\\S","\\d","\\D","\\A","\\Z","\\b","\\B","\\G","\\n","\\r","\\f","\\t")
c("\w","\W","\s","\S","\d","\D","\A","\Z","\b","\B","\G","\n","\r","\f","\t")

This results in the help file:

c("\\","\\","\\#","\#","\#")
c("\+","\?","\.","\*","\^","\$","\(","\)","\[","\]","\{","\}","\|","\_")
c("\+","\?","\.","\*","\^","\$","\(","\)","\[","\]","{","}","\|","\_")
c("\w","\W","\s","\S","\d","\D","\A","\Z","\b","\B","\G","\n","\r","\f","\t")
c("\w","\W","\s","\S","\d","\D","\A","\Z","\b","\B","\G","\n","\r","\f","\t")

Executed by example():

c("\\", "\\", "\\#", "#", "#")
c("+", "?", ".", "*", "^", "$", "(", ")", "[", "]", "{", "}", "|", "_")
c("+", "?", ".", "*", "^", "$", "(", ")", "[", "]", "{", "}", "|", "_")
c("w", "W", "s", "S", "d", "D", "A", "Z", "\b", "B", "G", "\n", "\r", "\f", 
"    ")
c("w", "W", "s", "S", "d", "D", "A", "Z", "\b", "B", "G", "\n", "\r", "\f", 
"    ")

When going from the .Rd file to the help file, single backslashes are left 
as-is but double backslashes are converted to singles. So to get a double 
backslash one has to start with four backslashes. This is fine.

When going from the help file to rendered code, double backlashes are now 
left as-is. Single backslashes are removed, *except* for "\b", "\n", "\r" 
and "\f". Escaped t's, "\t" are converted to actual tabs.

Is this all as it is supposed to be? The conversion of \t to <tab> does 
seem a potential problem.

The conversion of \t to <tab> occurs in other examples of rendering code 
also, for example:

 > myfun <- function(x,pattern="\t") "hello"
 > args(myfun)
function (x, pattern = "        ")

Best
Gordon
1 day later
#
Going from help to rendered code means via example()?  What happens
there is that is R-ex file is *sourced*, i.e., parsed and evaluated, so
what we see is what R does.
I cannot reproduce this: if I add

f <- function(x = "\t") "hello"
args(f)

to eda/R-ex/line.R and do example("line", "eda") I get

line> f <- function(x = "\t") "hello"

line> args(f)
function (x = "\t") 
NULL

without the tab being expanded ...

Best
-k
#
At 05:56 PM 9/06/2003, Kurt Hornik wrote:
Fair enough. What was confusing me was that what is echoed by example() is 
not exactly the same as what is in the R-ex file. I guess what happens is 
that the commands are cleaned up by removing extraneous backlashes. So what 
is executed by 'example()' is not necessarily identical as a string of 
characters to what is in the R-ex file but is functionally equivalent to it 
as an R expression.

There seems to be a separate issue with tabs in Windows, see below.
The expansion of \t to tab may be specific to Windows. The above examples 
reproduce for me in
Windows (R 1.7.0 patched or R 1.6.2) but don't reproduce in linux (R 
1.7.0). The simplest example of expansion in R for Windows (R 1.7.0 patched 
or R 1.6.2) is to type

"\t"

at the R prompt. This produces the printout

[1] "   "

which contains an actual tab. In R 1.7.0 for linux typing

"\t"

at the R prompt prints as

[1] "\t"

as expected.
Gordon