|
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? Best -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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 Wincent,
Have a look at: ?file.path ----------------Contact Details:------------------------------------------------------- Contact me: [hidden email] | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? > > Best > > -- > Wincent Ronggui HUANG > Sociology Department of Fudan University > PhD of City University of Hong Kong > http://homepage.fudan.edu.cn/rghuang/cv/ > > ______________________________________________ > [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. > [[alternative HTML version deleted]] ______________________________________________ [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 Wincent
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 ______________________________________________ [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 Tal Galili
Hmm, I don't think it gives what I want.
For example, I assign a file name to f, > f <- "a?b.txt" > file.path("e:",f) [1] "e:/a?b.txt" The resultant character is not accepted as a file name by Windows OS. On 9 May 2012 20:32, Tal Galili <[hidden email]> wrote: > Hi Wincent, > Have a look at: > ?file.path > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: [hidden email] | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > > > > On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? >> >> Best >> >> -- >> Wincent Ronggui HUANG >> Sociology Department of Fudan University >> PhD of City University of Hong Kong >> http://homepage.fudan.edu.cn/rghuang/cv/ >> >> ______________________________________________ >> [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. > > -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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 09.05.2012 17:14, Wincent wrote: > Hmm, I don't think it gives what I want. > > For example, I assign a file name to f, >> f<- "a?b.txt" >> file.path("e:",f) > [1] "e:/a?b.txt" > > The resultant character is not accepted as a file name by Windows OS. Not on Linux if you write to a smb file system, and that system won't tell you in advance. hence you have to know it yourself or correctly interpret the corresponding error messages. Uwe Ligges > On 9 May 2012 20:32, Tal Galili<[hidden email]> wrote: >> Hi Wincent, >> Have a look at: >> ?file.path >> >> >> >> ----------------Contact >> Details:------------------------------------------------------- >> Contact me: [hidden email] | 972-52-7275845 >> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >> www.r-statistics.com (English) >> ---------------------------------------------------------------------------------------------- >> >> >> >> >> On Wed, May 9, 2012 at 11:03 AM, Wincent<[hidden email]> 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? >>> >>> Best >>> >>> -- >>> Wincent Ronggui HUANG >>> Sociology Department of Fudan University >>> PhD of City University of Hong Kong >>> http://homepage.fudan.edu.cn/rghuang/cv/ >>> >>> ______________________________________________ >>> [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 Wincent
Why not just construct a valid file name and use that in cat? You can then use
file.path to join paths together if you want to write to a specific location, as in your example. steve -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Wincent Sent: Wednesday, May 09, 2012 11:15 AM To: Tal Galili Cc: r help Subject: Re: [R] file path Hmm, I don't think it gives what I want. For example, I assign a file name to f, > f <- "a?b.txt" > file.path("e:",f) [1] "e:/a?b.txt" The resultant character is not accepted as a file name by Windows OS. On 9 May 2012 20:32, Tal Galili <[hidden email]> wrote: > Hi Wincent, > Have a look at: > ?file.path > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: [hidden email] | 972-52-7275845 Read me: > www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------- > ------------------------ > > > > > On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? >> >> Best >> >> -- >> Wincent Ronggui HUANG >> Sociology Department of Fudan University PhD of City University of >> Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ >> >> ______________________________________________ >> [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. > > -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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. |
|
As I said, the file name is derived automatically from text processing.
Thanks all the same. On 10 May 2012 20:35, Upton, Stephen (Steve) (CIV) <[hidden email]> wrote: > Why not just construct a valid file name and use that in cat? You can then use > file.path to join paths together if you want to write to a specific location, > as in your example. > > steve > > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On > Behalf Of Wincent > Sent: Wednesday, May 09, 2012 11:15 AM > To: Tal Galili > Cc: r help > Subject: Re: [R] file path > > Hmm, I don't think it gives what I want. > > For example, I assign a file name to f, >> f <- "a?b.txt" >> file.path("e:",f) > [1] "e:/a?b.txt" > > The resultant character is not accepted as a file name by Windows OS. > > On 9 May 2012 20:32, Tal Galili <[hidden email]> wrote: >> Hi Wincent, >> Have a look at: >> ?file.path >> >> >> >> ----------------Contact >> Details:------------------------------------------------------- >> Contact me: [hidden email] | 972-52-7275845 Read me: >> www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >> www.r-statistics.com (English) >> ---------------------------------------------------------------------- >> ------------------------ >> >> >> >> >> On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? >>> >>> Best >>> >>> -- >>> Wincent Ronggui HUANG >>> Sociology Department of Fudan University PhD of City University of >>> Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ >>> >>> ______________________________________________ >>> [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. >> >> > > > > -- > Wincent Ronggui HUANG > Sociology Department of Fudan University PhD of City University of Hong Kong > http://homepage.fudan.edu.cn/rghuang/cv/ > > ______________________________________________ > [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. -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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. |
|
Has any mentioned
?make.names On Thu, May 10, 2012 at 8:39 AM, Wincent <[hidden email]> wrote: > As I said, the file name is derived automatically from text processing. > Thanks all the same. > > On 10 May 2012 20:35, Upton, Stephen (Steve) (CIV) <[hidden email]> wrote: >> Why not just construct a valid file name and use that in cat? You can then use >> file.path to join paths together if you want to write to a specific location, >> as in your example. >> >> steve >> >> -----Original Message----- >> From: [hidden email] [mailto:[hidden email]] On >> Behalf Of Wincent >> Sent: Wednesday, May 09, 2012 11:15 AM >> To: Tal Galili >> Cc: r help >> Subject: Re: [R] file path >> >> Hmm, I don't think it gives what I want. >> >> For example, I assign a file name to f, >>> f <- "a?b.txt" >>> file.path("e:",f) >> [1] "e:/a?b.txt" >> >> The resultant character is not accepted as a file name by Windows OS. >> >> On 9 May 2012 20:32, Tal Galili <[hidden email]> wrote: >>> Hi Wincent, >>> Have a look at: >>> ?file.path >>> >>> >>> >>> ----------------Contact >>> Details:------------------------------------------------------- >>> Contact me: [hidden email] | 972-52-7275845 Read me: >>> www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >>> www.r-statistics.com (English) >>> ---------------------------------------------------------------------- >>> ------------------------ >>> >>> >>> >>> >>> On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? >>>> >>>> Best >>>> >>>> -- >>>> Wincent Ronggui HUANG >>>> Sociology Department of Fudan University PhD of City University of >>>> Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ >>>> >>>> ______________________________________________ >>>> [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. >>> >>> >> >> >> >> -- >> Wincent Ronggui HUANG >> Sociology Department of Fudan University PhD of City University of Hong Kong >> http://homepage.fudan.edu.cn/rghuang/cv/ >> >> ______________________________________________ >> [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. > > > > -- > Wincent Ronggui HUANG > Sociology Department of Fudan University > PhD of City University of Hong Kong > http://homepage.fudan.edu.cn/rghuang/cv/ > > ______________________________________________ > [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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. ______________________________________________ [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
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 <[hidden email]> 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/ ______________________________________________ [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 jholtman
No, but make.names only makes syntactically valid names of R object.
I think I have mentioned what I did in the first email, although no concrete example was provided. Let me explain a bit. I have a text file like this, Title:AA? Content: 1xxxxxx1 ------------ Title:BA Content: 2xxxxxx2 I want to read the file in, process it and export it into two separate files. 1. d:/output/AA.txt with content of 1xxxxxx1, and 2. d:/output/BA with content of 2xxxxxx2. The first file name is AA because I know "AA?" is not a valid file name in Windows and ? is removed. However, there are lots of files and it will be convenient if I can convert any invalid file name (like "AA?") into a valid file name (like "AA") automatically. >From the answers provided by this mailing list, it seems not easy to write a function to construct valid file names because what is a valid file name depends on the OS and prior knowledge is needed. Best On 10 May 2012 20:53, jim holtman <[hidden email]> wrote: > Has any mentioned > > ?make.names > > On Thu, May 10, 2012 at 8:39 AM, Wincent <[hidden email]> wrote: >> As I said, the file name is derived automatically from text processing. >> Thanks all the same. >> >> On 10 May 2012 20:35, Upton, Stephen (Steve) (CIV) <[hidden email]> wrote: >>> Why not just construct a valid file name and use that in cat? You can then use >>> file.path to join paths together if you want to write to a specific location, >>> as in your example. >>> >>> steve >>> >>> -----Original Message----- >>> From: [hidden email] [mailto:[hidden email]] On >>> Behalf Of Wincent >>> Sent: Wednesday, May 09, 2012 11:15 AM >>> To: Tal Galili >>> Cc: r help >>> Subject: Re: [R] file path >>> >>> Hmm, I don't think it gives what I want. >>> >>> For example, I assign a file name to f, >>>> f <- "a?b.txt" >>>> file.path("e:",f) >>> [1] "e:/a?b.txt" >>> >>> The resultant character is not accepted as a file name by Windows OS. >>> >>> On 9 May 2012 20:32, Tal Galili <[hidden email]> wrote: >>>> Hi Wincent, >>>> Have a look at: >>>> ?file.path >>>> >>>> >>>> >>>> ----------------Contact >>>> Details:------------------------------------------------------- >>>> Contact me: [hidden email] | 972-52-7275845 Read me: >>>> www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | >>>> www.r-statistics.com (English) >>>> ---------------------------------------------------------------------- >>>> ------------------------ >>>> >>>> >>>> >>>> >>>> On Wed, May 9, 2012 at 11:03 AM, Wincent <[hidden email]> 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? >>>>> >>>>> Best >>>>> >>>>> -- >>>>> Wincent Ronggui HUANG >>>>> Sociology Department of Fudan University PhD of City University of >>>>> Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ >>>>> >>>>> ______________________________________________ >>>>> [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. >>>> >>>> >>> >>> >>> >>> -- >>> Wincent Ronggui HUANG >>> Sociology Department of Fudan University PhD of City University of Hong Kong >>> http://homepage.fudan.edu.cn/rghuang/cv/ >>> >>> ______________________________________________ >>> [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. >> >> >> >> -- >> Wincent Ronggui HUANG >> Sociology Department of Fudan University >> PhD of City University of Hong Kong >> http://homepage.fudan.edu.cn/rghuang/cv/ >> >> ______________________________________________ >> [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. > > > > -- > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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 Wincent
This works on Mac:
str <- "abc/d" gsub("/", "", str) Return: "abcd" Sent from my iPhone On May 14, 2012, at 4:28 AM, Wincent <[hidden email]> wrote: > 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 <[hidden email]> 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/ > > ______________________________________________ > [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. |
|
Emm, my bad.
I meant str <- "abc\d". Any ideas? On 14 May 2012 18:02, Baoqiang <[hidden email]> wrote: > This works on Mac: > > str <- "abc/d" > gsub("/", "", str) > > Return: > "abcd" > > > Sent from my iPhone > > On May 14, 2012, at 4:28 AM, Wincent <[hidden email]> wrote: > >> 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 <[hidden email]> 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/ >> >> ______________________________________________ >> [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. -- Wincent Ronggui HUANG Sociology Department of Fudan University PhD of City University of Hong Kong http://homepage.fudan.edu.cn/rghuang/cv/ ______________________________________________ [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 14-05-2012, at 12:07, Wincent wrote: > Emm, my bad. > I meant str <- "abc\d". > Any ideas? gsub("\\\\", "", str) Berend ______________________________________________ [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 May 14, 2012, at 6:35 AM, Berend Hasselman wrote: > > On 14-05-2012, at 12:07, Wincent wrote: > >> Emm, my bad. >> I meant str <- "abc\d". >> Any ideas? > > gsub("\\\\", "", str) #1: One cannot execute: str <- "abc\d" , at least on my machine, since that throws an error because "\d" is an "unrecognized escape". #2: If the string has a backslash as its fourth character then it would need to be created with: str <- "abc\\d" (Then Berend's gsub would succeed.) #3: If the string contains an ASCII cntrl-d. then the needed gsub command would be: str <- "abc\004" gsub("\\\004", "new", str) [1] "abcnew" -- David Winsemius, MD West Hartford, CT ______________________________________________ [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 |
