Function for concatenating paths?

Is there an existing function for concatenating paths?

I know that this is not so difficult to implement, but still ... in addition to take care of the completion of / (or \ ), I will need to take care of correctly determining the format of the OS path (i.e. whether we write C:\dir\file or /dir/file ).

As I said, I believe that I know how to implement it; Q: Should I do this? Functionality already exists in existing package R?

+37
r path concatenation
Oct 28 '12 at 15:19
source share
2 answers

Yes file.path()

 R> file.path("usr", "local", "lib") [1] "usr/local/lib" R> 

There is also equally useful system.path() for files in a package:

 R> system.file("extdata", "date_time_zonespec.csv", package="RcppBDT") [1] "/usr/local/lib/R/site-library/RcppBDT/extdata/date_time_zonespec.csv" R> 

which will receive the extdata/date_time_zonespec.csv regardless

  • where is the package installed and
  • OS

which is very convenient. Finally, there are

 R> .Platform$file.sep [1] "/" R> 

if you insist on doing it manually.

+66
Oct 28 '12 at 15:21
source share

In case anyone wants, this is my own path.cat function. Its functionality is comparable to Python os.path.join with the extra sugar that it interprets ..

With this function, you can build paths hierarchically, but unlike file.path , you leave the user the ability to redefine the hierarchy by setting an absolute path. And as added sugar, he can put ".." wherever he likes along the way, with obvious meaning.

eg.

  • path.cat("/home/user1","project/data","../data2") yelds /home/user1/project/data2

  • path.cat("/home/user1","project/data","/home/user2/data") yelds /home/user2/data

The function only works with slashes as a path separator, which is fine, since R transparently translates them into a backslash on a Windows machine.

 library("iterators") # After writing this function I've learned, that iterators are very inefficient in R. library("itertools") #High-level function that inteligentely concatenates paths given in arguments #The user interface is the same as for file.path, with the exception that it understands the path ".." #and it can identify relative and absolute paths. #Absolute paths starts comply with "^\/" or "^\d:\/" regexp. #The concatenation starts from the last absolute path in arguments, or the first, if no absolute paths are given. path.cat<-function(...) { elems<-list(...) elems<-as.character(elems) elems<-elems[elems!='' && !is.null(elems)] relems<-rev(elems) starts<-grep('^[/\\]',relems)[1] if (!is.na(starts) && !is.null(starts)) { relems<-relems[1:starts] } starts<-grep(':',relems,fixed=TRUE) if (length(starts)==0){ starts=length(elems)-length(relems)+1 }else{ starts=length(elems)-starts[[1]]+1} elems<-elems[starts:length(elems)] path<-do.call(file.path,as.list(elems)) elems<-strsplit(path,'[/\\]',FALSE)[[1]] it<-ihasNext(iter(elems)) out<-rep(NA,length(elems)) i<-1 while(hasNext(it)) { item<-nextElem(it) if(item=='..') { i<-i-1 } else if (item=='' & i!=1) { #nothing } else { out[i]<-item i<-i+1 } } do.call(file.path,as.list(out[1:i-1])) } 
+2
Jan 23 '14 at 4:00 p.m.
source share



All Articles