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

Hans-Peter gchappi at gmail.com
Fri Jun 10 11:16:22 CEST 2005


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 ;-(

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).

- 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




More information about the R-help mailing list