Bug 14846 - edit() and backquoted strings with backslashes
edit() and backquoted strings with backslashes
Status: RESOLVED FIXED
Product: R
Classification: Unclassified
Component: Low-level
R 2.14.1
All All
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-03-15 10:18 UTC by Andrew Runnalls
Modified: 2012-03-30 20:07 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Runnalls 2012-03-15 10:18:15 UTC
I encountered this bug doing the following (with R 2.14.1 on Linux using xemacs as the editor):

> trace(tools::Rd2txt, edit=TRUE)
Loading required package: tools
# Edit and save file
Error: '\d' is an unrecognized escape in character string starting "\d"
> 

It isn't actually necessary to make any modifications with the editor for this to occur: exiting straight away still causes it.

I believe the underlying problem is that when an object is being prepared for editing, the deparser does not correctly handle backslashes within backquoted strings.  (If in the example above, the editor is used to double up all such backslashes before saving, then the error does not arise.)

Andrew
Comment 1 Peter Dalgaard 2012-03-29 12:34:45 UTC
Simpler version:


> f <- function(x) switch(x,"\\dbc"=2,3)
> dput(f)
function (x) 
switch(x, `\dbc` = 2, 3)
> f
function(x) switch(x,"\\dbc"=2,3)
> deparse(f)
[1] "function (x) "             "switch(x, `\\dbc` = 2, 3)"
> parse(text=deparse(f))
Error: '\d' is an unrecognized escape in character string starting "\d"


I.e. deparse/dput seems indeed to have a problem
Comment 2 Duncan Murdoch 2012-03-30 20:07:18 UTC
Fixed in R-devel and R-patched.
Comment 3 Duncan Murdoch 2012-03-30 23:22:22 UTC
On 29/03/2012 7:34 AM, r-bugs@r-project.org wrote:
> https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14846
>
> Peter Dalgaard<pd.mes@cbs.dk>  changed:
>
>             What    |Removed                     |Added
> ----------------------------------------------------------------------------
>                   CC|                            |pd.mes@cbs.dk
>
> --- Comment #1 from Peter Dalgaard<pd.mes@cbs.dk>  2012-03-29 07:34:45 EDT ---
> Simpler version:
>
>
> >  f<- function(x) switch(x,"\\dbc"=2,3)
> >  dput(f)
> function (x)
> switch(x, `\dbc` = 2, 3)
> >  f
> function(x) switch(x,"\\dbc"=2,3)
> >  deparse(f)
> [1] "function (x) "             "switch(x, `\\dbc` = 2, 3)"
> >  parse(text=deparse(f))
> Error: '\d' is an unrecognized escape in character string starting "\d"
>
>
> I.e. deparse/dput seems indeed to have a problem


I think I have this fixed now.  The same problem showed up in a number 
of locations in the deparse code, and I have tried to catch them all.

The idea is to use the EncodeString function from the print routines; it 
knows how to escape things and add quotes.

Will commit to R-devel and R-patched after some testing...

Duncan Murdoch