|
Hi,
I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. Example failed attempts > sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") Error: '\C' is an unrecognized escape in character string starting "G:\C" > sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=TRUE) Error: '\C' is an unrecognized escape in character string starting "G:\C" > sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=FALSE) Error: '\C' is an unrecognized escape in character string starting "G:\C") > sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") Error: unexpected symbol in "sub("/","\","G" Thanks. Dan ______________________________________________ [hidden email] 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. |
|
On 12-06-14 6:23 PM, Lopez, Dan wrote:
> Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C" This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Hello,
See inline. Em 15-06-2012 08:03, Duncan Murdoch escreveu: > On 12-06-14 6:23 PM, Lopez, Dan wrote: >> Hi, >> >> I have been trying to get the sub function to work but can't for the >> life of me figure it out. I tried looking at ?sub but I think there is >> something that I am just not understanding. >> >> Example failed attempts >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: '\C' is an unrecognized escape in character string starting "G:\C" > > This isn't anything to do with sub(), it's the third string that's the > problem. When you say "G:\Comp..." it thinks you've escaped the C, but > there's no such escape. > > Duncan Murdoch > This can be seen if we try to assign the second string. s1 <- "G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb" Error: '\C' is an unrecognized escape in character string starting "G:\C" The right way would be s2 <- 'G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb' pattern <- '\\\\' gsub(pattern, '/', s2) [1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" Note also the use of 'gsub', Global sub. I believe it's obvious that the purpose is to substitute all backslashes by forward ones. Hope this helps, Rui Barradas >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=TRUE) >> Error: '\C' is an unrecognized escape in character string starting "G:\C" >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=FALSE) >> Error: '\C' is an unrecognized escape in character string starting >> "G:\C") >> >>> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: unexpected symbol in "sub("/","\","G" >> >> Thanks. >> Dan >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Duncan Murdoch-2
Hi Duncan,
That's right but I want to be able to use this to replace back slashes with front slashes when I copy and paste file paths. How can I make this work so I don't get this error? Thanks. Dan -----Original Message----- From: Duncan Murdoch [mailto:[hidden email]] Sent: Friday, June 15, 2012 12:04 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub On 12-06-14 6:23 PM, Lopez, Dan wrote: > Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C" This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting > "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Hi,
I guess you need something like this: > str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb" > gsub('\\\\','/',str1) [1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" A.K. ----- Original Message ----- From: "Lopez, Dan" <[hidden email]> To: Duncan Murdoch <[hidden email]> Cc: R help <[hidden email]> Sent: Monday, June 18, 2012 10:37 AM Subject: Re: [R] Using Sub Hi Duncan, That's right but I want to be able to use this to replace back slashes with front slashes when I copy and paste file paths. How can I make this work so I don't get this error? Thanks. Dan -----Original Message----- From: Duncan Murdoch [mailto:[hidden email]] Sent: Friday, June 15, 2012 12:04 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub On 12-06-14 6:23 PM, Lopez, Dan wrote: > Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C" This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting > "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Hi A.K.,
That works but when I copy and paste from Window explorer it will have a single backslashes and not double. It works with double as what you have in your example below but not with single as what I would end up passing. Do you have any other suggestions or methods to accomplish this? Thanks. Dan -----Original Message----- From: arun [mailto:[hidden email]] Sent: Monday, June 18, 2012 8:59 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub Hi, I guess you need something like this: > str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb" > gsub('\\\\','/',str1) [1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" A.K. ----- Original Message ----- From: "Lopez, Dan" <[hidden email]> To: Duncan Murdoch <[hidden email]> Cc: R help <[hidden email]> Sent: Monday, June 18, 2012 10:37 AM Subject: Re: [R] Using Sub Hi Duncan, That's right but I want to be able to use this to replace back slashes with front slashes when I copy and paste file paths. How can I make this work so I don't get this error? Thanks. Dan -----Original Message----- From: Duncan Murdoch [mailto:[hidden email]] Sent: Friday, June 15, 2012 12:04 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub On 12-06-14 6:23 PM, Lopez, Dan wrote: > Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C" This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting > "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Hi Dan,
I am using linux. So, not sure whether this will work or you tried it before. After you paste the path at (1:), then press enter two times. One more point. In your path, there were spaces within slashes. So, if you do the scan, it will return as 5 items, which you have to join again. I removed the spaces and tried this. But you can try. str=scan(what="character",allowEscapes=F) 1: G:\CompensationAudits_Reports\StepAudit\StepsoffStep.accdb 2: Read 1 item > str [1] "G:\\CompensationAudits_Reports\\StepAudit\\StepsoffStep.accdb" > gsub('\\\\', '/',str) [1] "G:/CompensationAudits_Reports/StepAudit/StepsoffStep.accdb" A.K. ----- Original Message ----- From: "Lopez, Dan" <[hidden email]> To: arun <[hidden email]> Cc: R help <[hidden email]> Sent: Monday, June 18, 2012 12:03 PM Subject: RE: [R] Using Sub Hi A.K., That works but when I copy and paste from Window explorer it will have a single backslashes and not double. It works with double as what you have in your example below but not with single as what I would end up passing. Do you have any other suggestions or methods to accomplish this? Thanks. Dan -----Original Message----- From: arun [mailto:[hidden email]] Sent: Monday, June 18, 2012 8:59 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub Hi, I guess you need something like this: > str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb" > gsub('\\\\','/',str1) [1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" A.K. ----- Original Message ----- From: "Lopez, Dan" <[hidden email]> To: Duncan Murdoch <[hidden email]> Cc: R help <[hidden email]> Sent: Monday, June 18, 2012 10:37 AM Subject: Re: [R] Using Sub Hi Duncan, That's right but I want to be able to use this to replace back slashes with front slashes when I copy and paste file paths. How can I make this work so I don't get this error? Thanks. Dan -----Original Message----- From: Duncan Murdoch [mailto:[hidden email]] Sent: Friday, June 15, 2012 12:04 AM To: Lopez, Dan Cc: R help Subject: Re: [R] Using Sub On 12-06-14 6:23 PM, Lopez, Dan wrote: > Hi, > > I have been trying to get the sub function to work but can't for the life of me figure it out. I tried looking at ?sub but I think there is something that I am just not understanding. > > Example failed attempts >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: '\C' is an unrecognized escape in character string starting "G:\C" This isn't anything to do with sub(), it's the third string that's the problem. When you say "G:\Comp..." it thinks you've escaped the C, but there's no such escape. Duncan Murdoch > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=TRUE) > Error: '\C' is an unrecognized escape in character string starting "G:\C" > >> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb",fixed=FALSE) > Error: '\C' is an unrecognized escape in character string starting > "G:\C") > >> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >> Step.accdb") > Error: unexpected symbol in "sub("/","\","G" > > Thanks. > Dan > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Lopez, Dan
Please study how escapes work. The representation you see in the source code is not the same as what is actually stored in memory.
As to your problems with the clipboard, I recommend writing scripts that do not interact with the clipboard directly. Violating this rule puts an error-prone human in the loop for repeating the script, and will eventually lead to irreproducible results. That said, using the clipboard to create the script itself is fine. I usually just edit Windows paths in scripts to have forward slashes or double backslashes, and I use relative paths liberally. You can also play with file.choose() or readLines("clipboard") to get source-compatible paths. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Lopez, Dan" <[hidden email]> wrote: >Hi A.K., > >That works but when I copy and paste from Window explorer it will have >a single backslashes and not double. It works with double as what you >have in your example below but not with single as what I would end up >passing. > >Do you have any other suggestions or methods to accomplish this? > >Thanks. >Dan > > >-----Original Message----- >From: arun [mailto:[hidden email]] >Sent: Monday, June 18, 2012 8:59 AM >To: Lopez, Dan >Cc: R help >Subject: Re: [R] Using Sub > >Hi, > >I guess you need something like this: > >> str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off >Step.accdb" >> gsub('\\\\','/',str1) >[1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" > > >A.K. > > > > >----- Original Message ----- >From: "Lopez, Dan" <[hidden email]> >To: Duncan Murdoch <[hidden email]> >Cc: R help <[hidden email]> >Sent: Monday, June 18, 2012 10:37 AM >Subject: Re: [R] Using Sub > >Hi Duncan, > >That's right but I want to be able to use this to replace back slashes >with front slashes when I copy and paste file paths. How can I make >this work so I don't get this error? > >Thanks. >Dan > >-----Original Message----- >From: Duncan Murdoch [mailto:[hidden email]] >Sent: Friday, June 15, 2012 12:04 AM >To: Lopez, Dan >Cc: R help >Subject: Re: [R] Using Sub > >On 12-06-14 6:23 PM, Lopez, Dan wrote: >> Hi, >> >> I have been trying to get the sub function to work but can't for the >life of me figure it out. I tried looking at ?sub but I think there is >something that I am just not understanding. >> >> Example failed attempts >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: '\C' is an unrecognized escape in character string starting >"G:\C" > >This isn't anything to do with sub(), it's the third string that's the >problem. When you say "G:\Comp..." it thinks you've escaped the C, but >there's no such escape. > >Duncan Murdoch > >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=TRUE) >> Error: '\C' is an unrecognized escape in character string starting >"G:\C" >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=FALSE) >> Error: '\C' is an unrecognized escape in character string starting >> "G:\C") >> >>> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: unexpected symbol in "sub("/","\","G" >> >> Thanks. >> Dan >> >> ______________________________________________ >> [hidden email] 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. > >______________________________________________ >[hidden email] 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. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
|
The parser (the part of R that interprets text as R code) interprets things like
"\t" as the tab character and there is no way to avoid that. However, when the functions readLines(), scan(), and readline() handle input they do not assign any special meanings to backslashes. Hence, if you want to be able to copy and paste entries from other Windows applications to R you can do it by using scan(), etc. E.g., > readline(prompt="File path? ") File path? C:\Documents and Settings\Bill Dunlap\My Documents\the data.csv [1] "C:\\Documents and Settings\\Bill Dunlap\\My Documents\\the data.csv" > gsub("\\\\", "/", .Last.value) [1] "C:/Documents and Settings/Bill Dunlap/My Documents/the data.csv" It is not terribly convenient, but it is possible. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of Jeff Newmiller > Sent: Monday, June 18, 2012 10:29 AM > To: Daniel Lopez; arun > Cc: R help > Subject: Re: [R] Using Sub > > Please study how escapes work. The representation you see in the source code is not the > same as what is actually stored in memory. > > As to your problems with the clipboard, I recommend writing scripts that do not interact > with the clipboard directly. Violating this rule puts an error-prone human in the loop for > repeating the script, and will eventually lead to irreproducible results. > > That said, using the clipboard to create the script itself is fine. I usually just edit Windows > paths in scripts to have forward slashes or double backslashes, and I use relative paths > liberally. You can also play with file.choose() or readLines("clipboard") to get source- > compatible paths. > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > "Lopez, Dan" <[hidden email]> wrote: > > >Hi A.K., > > > >That works but when I copy and paste from Window explorer it will have > >a single backslashes and not double. It works with double as what you > >have in your example below but not with single as what I would end up > >passing. > > > >Do you have any other suggestions or methods to accomplish this? > > > >Thanks. > >Dan > > > > > >-----Original Message----- > >From: arun [mailto:[hidden email]] > >Sent: Monday, June 18, 2012 8:59 AM > >To: Lopez, Dan > >Cc: R help > >Subject: Re: [R] Using Sub > > > >Hi, > > > >I guess you need something like this: > > > >> str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off > >Step.accdb" > >> gsub('\\\\','/',str1) > >[1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" > > > > > >A.K. > > > > > > > > > >----- Original Message ----- > >From: "Lopez, Dan" <[hidden email]> > >To: Duncan Murdoch <[hidden email]> > >Cc: R help <[hidden email]> > >Sent: Monday, June 18, 2012 10:37 AM > >Subject: Re: [R] Using Sub > > > >Hi Duncan, > > > >That's right but I want to be able to use this to replace back slashes > >with front slashes when I copy and paste file paths. How can I make > >this work so I don't get this error? > > > >Thanks. > >Dan > > > >-----Original Message----- > >From: Duncan Murdoch [mailto:[hidden email]] > >Sent: Friday, June 15, 2012 12:04 AM > >To: Lopez, Dan > >Cc: R help > >Subject: Re: [R] Using Sub > > > >On 12-06-14 6:23 PM, Lopez, Dan wrote: > >> Hi, > >> > >> I have been trying to get the sub function to work but can't for the > >life of me figure it out. I tried looking at ?sub but I think there is > >something that I am just not understanding. > >> > >> Example failed attempts > >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off > >>> Step.accdb") > >> Error: '\C' is an unrecognized escape in character string starting > >"G:\C" > > > >This isn't anything to do with sub(), it's the third string that's the > >problem. When you say "G:\Comp..." it thinks you've escaped the C, but > >there's no such escape. > > > >Duncan Murdoch > > > >> > >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off > >>> Step.accdb",fixed=TRUE) > >> Error: '\C' is an unrecognized escape in character string starting > >"G:\C" > >> > >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off > >>> Step.accdb",fixed=FALSE) > >> Error: '\C' is an unrecognized escape in character string starting > >> "G:\C") > >> > >>> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off > >>> Step.accdb") > >> Error: unexpected symbol in "sub("/","\","G" > >> > >> Thanks. > >> Dan > >> > >> ______________________________________________ > >> [hidden email] 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. > > > >______________________________________________ > >[hidden email] 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. > > > >______________________________________________ > >[hidden email] 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. > > ______________________________________________ > [hidden email] 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. [hidden email] 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. |
|
In reply to this post by Jeff Newmiller
Jeff - Thanks. I think I'll just stick to using file.choose().
Dan -----Original Message----- From: Jeff Newmiller [mailto:[hidden email]] Sent: Monday, June 18, 2012 10:29 AM To: Lopez, Dan; arun Cc: R help Subject: Re: [R] Using Sub Please study how escapes work. The representation you see in the source code is not the same as what is actually stored in memory. As to your problems with the clipboard, I recommend writing scripts that do not interact with the clipboard directly. Violating this rule puts an error-prone human in the loop for repeating the script, and will eventually lead to irreproducible results. That said, using the clipboard to create the script itself is fine. I usually just edit Windows paths in scripts to have forward slashes or double backslashes, and I use relative paths liberally. You can also play with file.choose() or readLines("clipboard") to get source-compatible paths. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Lopez, Dan" <[hidden email]> wrote: >Hi A.K., > >That works but when I copy and paste from Window explorer it will have >a single backslashes and not double. It works with double as what you >have in your example below but not with single as what I would end up >passing. > >Do you have any other suggestions or methods to accomplish this? > >Thanks. >Dan > > >-----Original Message----- >From: arun [mailto:[hidden email]] >Sent: Monday, June 18, 2012 8:59 AM >To: Lopez, Dan >Cc: R help >Subject: Re: [R] Using Sub > >Hi, > >I guess you need something like this: > >> str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off >Step.accdb" >> gsub('\\\\','/',str1) >[1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb" > > >A.K. > > > > >----- Original Message ----- >From: "Lopez, Dan" <[hidden email]> >To: Duncan Murdoch <[hidden email]> >Cc: R help <[hidden email]> >Sent: Monday, June 18, 2012 10:37 AM >Subject: Re: [R] Using Sub > >Hi Duncan, > >That's right but I want to be able to use this to replace back slashes >with front slashes when I copy and paste file paths. How can I make >this work so I don't get this error? > >Thanks. >Dan > >-----Original Message----- >From: Duncan Murdoch [mailto:[hidden email]] >Sent: Friday, June 15, 2012 12:04 AM >To: Lopez, Dan >Cc: R help >Subject: Re: [R] Using Sub > >On 12-06-14 6:23 PM, Lopez, Dan wrote: >> Hi, >> >> I have been trying to get the sub function to work but can't for the >life of me figure it out. I tried looking at ?sub but I think there is >something that I am just not understanding. >> >> Example failed attempts >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: '\C' is an unrecognized escape in character string starting >"G:\C" > >This isn't anything to do with sub(), it's the third string that's the >problem. When you say "G:\Comp..." it thinks you've escaped the C, but >there's no such escape. > >Duncan Murdoch > >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=TRUE) >> Error: '\C' is an unrecognized escape in character string starting >"G:\C" >> >>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb",fixed=FALSE) >> Error: '\C' is an unrecognized escape in character string starting >> "G:\C") >> >>> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off >>> Step.accdb") >> Error: unexpected symbol in "sub("/","\","G" >> >> Thanks. >> Dan >> >> ______________________________________________ >> [hidden email] 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. > >______________________________________________ >[hidden email] 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. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Lopez, Dan
Possibly overkill, I use a Windows batch file that calls a perl script.
Both the bat and pl files need to be in the executable path. Open a command prompt, type `pwd ' and then drag the file or folder from the Windows Explorer to the cmd. The script will return the file with backslashes converted to forward slashes. Then I copy and paste from the cmd window into my R script. c:\>more c:\bin\batch\pwd.BAT @echo off c:\bin\batch\pwd.pl %1 c:\>more c:\bin\batch\pwd.pl #! /usr/bin/perl -w # prints pwd with forward slashes for R use File::Basename; #print $#ARGV; if ($#ARGV == -1) { $pwd = `cd`; chomp $pwd; } else { $pwd = $ARGV[0]; } $pwd =~ s|\\|\/|g; print "$pwd"; -P. ************************************************************** Philip M. Hurvitz, PhD | Research Assistant Professor | UW-CBE Urban Form Lab | 1107 NE 45th Street, Suite 535 | Box 354802 University of Washington, Seattle, Washington 98195-4802, USA [hidden email] | http://gis.washington.edu/phurvitz "What is essential is invisible to the eye." -de Saint-Exupéry ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
