[R] how to replace a single backward slash with a doublebackward slash?

William Dunlap wdunlap at tibco.com
Mon Dec 14 01:16:32 CET 2009



Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Sean Zhang
> Sent: Sunday, December 13, 2009 4:01 PM
> To: David Winsemius
> Cc: r-help at r-project.org
> Subject: Re: [R] how to replace a single backward slash with 
> a doublebackward slash?
> 
> David and William,
> Thanks for your reply which make me know the concept of 
> escape symbols.
> 
> As David guessed, I was trying to write a function which will
> accept a path cut from windows explorer.
> and as you know windows explorer uses "\".
> 
> e.g., c:\temp\function.r
> 
> I originally would like that the function is able to change 
> the example path
> into "c:/temp/function.r"
> David's final comment seems to suggest this is impossible...
> If so, it is a limitation because I have to manually change 
> "\" into "/"
> each time.

You can use scan to avoid manually changing the backslashes
to slashes.  E.g.,
   > z <- scan(what="",sep="\n")
   1: \\somecomputer\e\Documents and Settings\John Q Public\My
Documents\The Results.pdf
   2: 
   Read 1 item
   > z
   [1] "\\\\somecomputer\\e\\Documents and Settings\\John Q Public\\My
Documents\\The Results.pdf"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> But it is good to know this limitation.
> 
> Correct me, if I misunderstand and there is no such a limitation.
> 
> Thanks again.
> 
> -Sean
> 
> 
> 
> 
> 
> 
> 
> 
> On Sun, Dec 13, 2009 at 5:26 PM, David Winsemius 
> <dwinsemius at comcast.net>wrote:
> 
> >
> > On Dec 13, 2009, at 5:11 PM, Sean Zhang wrote:
> >
> >  Dear R-helpers:
> >>
> >> Hours ago, I asked how to replace a single forward slash 
> with a double
> >> backward slash and recieved great help. Thanks again for 
> all the repliers.
> >>
> >> In the meantime, I wonder how to replace a single backward 
> slash with a
> >> double backward slash?
> >>
> >> e.g., I want change "c:\test" into "c:\\test"
> >>
> >> I tried the following but does not work.
> >> gsub("\\\","\\\\",)
> >>
> >> Can someone help?
> >>
> >
> > Your problem may be that you think there actually is a "\" 
> in "c:\test".
> > There isn't:
> >
> > > grep("\\\\", "c:\test")  # which would have found a true "\"
> > integer(0)
> >
> > It's an escaped "t", which is the tab character = "\t":
> >
> > > grep("\\\t", "c:\test")
> > [1] 1
> > > cat("rr\tqq")
> > rr      qq
> >
> > If your goal is to make file paths in Windows correctly, 
> then you have two
> > choices:
> >
> > a) use doubled "\\"'s in the literal strings you type, or ...
> > b) use "/"'s
> >
> > So maybe you should explain what you are doing? We don't 
> request that
> > background out of nosiness, but rather so we can give better answers
> >
> > --
> >
> > David Winsemius, MD
> > Heritage Laboratories
> > West Hartford, CT
> >
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> 




More information about the R-help mailing list