file path
Thanks for the suggestion. The file name in my case is Chinese, which makes the regular expression less useful. Anyway, I would like to pose a followup question. I have a character string of "ABC\D", and want to strip away the "\" and want a returned character of "ABCD". How can I do it with gsub() ? Thanks again.
On 9 May 2012 22:40, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
On 09/05/2012 4:03 AM, Wincent wrote:
Dear all, is there any function to assert whether a file path is legitimate, and to convert any potential file path to a legitimate file path? I automate a batch of files and write them to plain text files with cat(). The file argument of cat() is generated automatically which may contain characters such as ?< ?>, unacceptable in Windows OS. What I do at this moment is to strip such characters off with gsub(). Is there any direct way to make legitimate file path without detailed knowledge about the naming rule specific to a OS?
I would just try to create the file, and if you fail, it's not legitimate.
?Alternatively, you could look at the tests that R uses when it checks a
package: ?we try to keep filenames portable to all operating systems. ?The
rules seem to be strictest for vignettes:
? ? ? ?## we specify ASCII filenames starting with a letter in R-exts
? ? ? ?## do this in a locale-independent way.
? ? ? ?OK <-
grep("^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-]+$",
vignettes)
Duncan Murdoch
Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/