[R] dir() and RegEx and gsub()

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Jun 10 13:01:36 CEST 2005


Hans-Peter wrote:

> 2005/6/9, Sarah Goslee <sarah.goslee at gmail.com>:
> 
> 
>>Under R, for reasons I've never quite understood,
>>"\\." evaluates to .
> 
> 
> Thanks to the answers of B. Ripley and Gabor I think, I understand now:
> 
>   1)  the patter-string "\\.csv$" gives the regular expression "\.csv$"
>   2)  now the backslash lets the dot to be interpreted literally
>       (instad of a metacharacter). As said by Gabor, an alternative is,
>       to put the dot between brackets
> 
> 
>>If you mean you want to change the "\" to either "\\" or "/" I'm
>>really not sure.
> 
> 
> Yes that's what I intended. 
> Because in windwos I copy the path from the address bar of the
> explorer and paste it in the R console window. Now I *have* something
> like
> myfile <- "D:\UebungenNDK\DataMining\DataMiningSeries.r"
 > that needs to be adjusted manually ;-(


Copy and try
   myfile <- readLines("clipboard")




> I interpret the answers
> 
> 
>>>myfile <- "D:\UebungenNDK\DataMining\DataMiningSeries.r"
>>
>>I am sure that's not what you intended.  It has to be written as
>>[snip] [1]  "D:/UebungenNDK/DataMining/DataMiningSeries.r"
> 
> 
> and 
> 
> 
>>>myfile <- "D:\UebungenNDK\DataMining\DataMiningSeries.r"
>>
>>Variable myfile, as you have written it above, has no backslashes in it
>>so there is no way way to know where they are supposed to be. 
> 
> 
> that it's not possible at all to have a character string with
> backslashes in it (because they *will* act as escape characters).

It *is* possible to *have* such a character string,
   x <- "a\\b"
generates an x that contains a *single* backslash.
It's a matter of parsing.


> - Maybe I could get along by writing an *external* function that would
> give me back a proper formatted path, eg.
> fold <- as.path( "D:\UebungenNDK\DataMining\DataMiningSeries.r" )
> [1] "D:/UebungenNDK/DataMining/DataMiningSeries.r"
> 
> - But then the behaviour would be different from all the other strings
> in R which doesn't seem to be a good idea either
> 
> 
>>I've always just processed the strings in vim or similar, rather
> 
> than fight with R.
> 
> I do it in DOS-Batch files. Quite ugly but it works...
> 
> 
> Regards,
> Hans-Peter
> 
> ______________________________________________
> 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