[R] How to convert "c:\a\b" to "c:/a/b"?

Henrik Bengtsson hb at maths.lth.se
Tue Jun 28 09:09:04 CEST 2005


Mulholland, Tom wrote:
> 
>>-----Original Message-----
>>From: r-help-bounces at stat.math.ethz.ch
>>[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Henrik Bengtsson
>>Sent: Tuesday, 28 June 2005 2:54 AM
>>To: Spencer Graves
>>Cc: r-help at stat.math.ethz.ch; Dirk Eddelbuettel
>>Subject: Re: [R] How to convert "c:\a\b" to "c:/a/b"?
>>
> 
> ... snipped
>  
> 
>>  Thus, you cannot write your program such that it fools the parser, 
>>because your program is evaluated first after the parser.  In other 
>>words, there is no way you can get nchar("\n") to equal 2.
>>
> 
> 
> I had been waiting for this answer because it was the conclusion I had come to. Given that I mainly work in a windows world this has been a problem. For various reasons I receive files liberally sprinkled with such pathnames. I generally pre-process them using whatever is at hand. It's not a big problem, just annoying to have to explain to collegues that this is something R can't do. Not a good advertisment for those who have no idea about escape codes. 

Please, note this basically only applies to source(), expressions at the 
R prompt (and unfortunately read.table()), and therefore you should not 
have to pre-process you files.  Here are some illustrating example. It 
is a good exercise to convince yourself that you understand why you get 
the different results;

code <- "x <- \"D:\\spencerg\\statmtds\\R\\Rnews\""
cat(file="foo.R", code)

file.show("foo.R")   # x <- "D:\spencerg\statmtds\R\Rnews"

x <- NA
eval(parse(text=code))
print(x)
rm(x)
[1] "D:spencergstatmtdsRRnews"

source("foo.R")
print(x)
[1] "D:spencergstatmtdsRRnews"

print(readLines("foo.R"))
[1] "x <- \"D:\\spencerg\\statmtds\\R\\Rnews\""

print(scan("foo.R", what=character(0), allowEscapes=FALSE))
[1] "x"                        "<-"
[3] "D:spencergstatmtdsRRnews"

print(read.table("foo.R"))
   V1 V2                       V3
1  x <- D:spencergstatmtdsRRnews

print(readChar("foo.R", nchar=256))
[1] "x <- \"D:\\spencerg\\statmtds\\R\\Rnews\""

 > print(readBin("foo.R", what=integer(0), size=1, n=256))
[1]  120  32  60  45  32  34  68  58  92 115 112 101 110  99 101 114 103 
  92 115
[20] 116  97 116 109 116 100 115  92  82  92  82 110 101 119 115  34


Comment/suggestion: It would be nice if read.table() would pass argument 
'allowEscapes' (or just '...') to scan().

/Henrik

> However I can't believe that this problem cannot be solved. The thoughts that have come through my head are to write a c routine that effectively ignores the possibility that \n means newline and thus remaps all the escape codes into text (\\ and the character code.) 
> 
> I've never written in C which is one of the reasons that I have never attempted this. I would be interested in any thoughts about the viability of my proposal. It seems an awful lot of work (at least for someone who hasn't done this sort of stuff before) for something that can be achieved in many other ways.
> 
> Tom
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list