Re: [R] Store filename

From: Jim Lemon <jim_at_bitwrit.com.au>
Date: Sat, 07 Jun 2008 21:46:38 +1000

DAVID ARTETA GARCIA wrote:
> Hi list,
>
> Is it possible to save the name of a filename automatically when
> reading it using read.table() or some other function?
> My aim is to create then an output table with the name of the original
> table with a suffix like _out
>
> example:
>
> mydata = read.table("Run224_v2_060308.txt", sep = "\t", header = TRUE)
>
> ## store name?
>
> myfile = the_name_of_the_file
>
> ## do analysis of data and store in a data.frame "myoutput"
> ## write output in tab format
>
> write.table(myoutput, c(myfile,"_out.txt"),sep="\t")
>
> the name of the new file will be
>
> "Run224_v2_060308_out.txt"
>
Hi David,
If you look at the code in the "htmlize" function in the prettyR package, you can see how to break down a filename into its components and then assemble a filename that is the same except for the extension.

  # if there is no HTML base name, use the Rfile name   if(missing(HTMLbase)) {
   HTMLbase<-unlist(strsplit(basename(Rfile),"\\."))    HTMLbase<-HTMLbase[1:(length(HTMLbase)-1)]    if(missing(title)) title<-paste("Listing of",HTMLbase)   }
  # If there is no HTML directory, use the path on the Rfile   if(missing(HTMLdir)) {
   if(length(grep("/",Rfile))) HTMLdir<-unlist(strsplit(Rfile,"/"))    else HTMLdir<-unlist(strsplit(Rfile,"\\\\"))    HTMLdir<-
   ifelse(length(HTMLdir)==1,".",file.path(HTMLdir[-length(HTMLdir)]))   }
...
  # here we make the filename for the listing   listname<-paste(HTMLdir,"/",HTMLbase,".html",sep="",collapse="")

Jim



R-help_at_r-project.org 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. Received on Sat 07 Jun 2008 - 11:49:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 07 Jun 2008 - 12:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive