[Rd] file_path_as_absolute duplicates "/" (PR#14078)

joehl at web.de joehl at web.de
Sun Nov 22 19:05:16 CET 2009


Full_Name: Jens Oehlschlägel
Version: 2.10.0
OS: Windows XP
Submission from: (NULL) (85.181.157.36)


file_path_as_absolute duplicates "/" for files in the root path, 
which goes back to the fact that file.path(dirname(x), basename(x)) 
currently is not guaranteed to restore x


> x <- "d:/x.RDAta"
> file_path_as_absolute(x)
[1] "d://x.RData"
> file.path(dirname(x), basename(x))
[1] "d://x.RData"
> dirname(x)
[1] "d:/"

> x <- "/"
> file.path(dirname(x), basename(x))
[1] "//"

a possible fix would be to use gsub in file.path before returning, as in 
gsub("/+","/","d://x.RData")

This would 
- standardize the path returned, e.g. help searching for this path in another
one
- make sure we can use that path in setwd(), because
> setwd("//")
Error in setwd("//") : cannot change working directory
> file.create("//a.txt")
[1] FALSE
Warning message:
In file.create("//a.txt") :
  cannot create file '//a.txt', reason 'Invalid argument'



Also note that the help on basename says

"
basename removes all of the path up to the last path separator (if any).
dirname returns the part of the path up to (but excluding) the last path
separator, or "." if there is no path separator. 
"

but obviously there is an undocumented exception for the root path (I guess to
have dirname always return a valid path that we can use in e.g. setwd())

> basename("/")
[1] ""
> dirname("/")
[1] "/"


It is not easy to understand that dirname/basename
- neither always split a string into a path and a file component (since in
"/a/b/" it avoids empty basename and "b" goes to basename)
- nor always split a string into the last token in basename and the rest in
dirname (since it avoids empty rest in "/")
So whatever the rules are, it would be helpful to find them in the help,
because
- not everyone knows what basename / dirname usually do under unix (IEEE Std
1003.1)
- R is not following IEEE Std 1003.1 (since basename("/")=="" while IEEE
requires "/")

Help should also explain why and what happens to "\\\\servername", is that
dirname, is that basename? Currently we have
> dirname(x)
[1] "."
> basename(x)
[1] "\\\\asdfasdf"
> file.path(dirname(x), basename(x))
[1] "./\\\\asdfasdf"


> version
               _                            
platform       i386-pc-mingw32              
arch           i386                         
os             mingw32                      
system         i386, mingw32                
status                                      
major          2                            
minor          10.0                         
year           2009                         
month          10                           
day            26                           
svn rev        50208                        
language       R                            
version.string R version 2.10.0 (2009-10-26)



More information about the R-devel mailing list