[R] please help, iteration through a list of files and plot each one

Shane McMahon shanemcmahon at wisc.edu
Tue Apr 2 08:12:28 CEST 2013


There's probably a better way to do it, but here's one way:

require(stringr)
windows()
file_list <- list.files(pattern=".txt")
  
for (file in file_list)
{
        
  
   
  dataset <- read.table(file, header=TRUE, sep="\t")


  plot(dataset)
  savePlot(filename=str_replace(file,".txt",""),type="png")
   
}






On 4/1/2013 11:13 PM, David Lyon wrote:
>
> I have many files in 1 directory, file names end in .txt.
>
> Each file has 2 columns
>
> col1 col2
>
> 2 3
>
> 3 4
>
> 4 5
>
> 5 6
>
>
> I want to make a list of the file names and iterate through each plotting them in a separate file $filename\.png with the png swapped for txt.
>
> So far I have this, can someone help fill in the blanks?
>
> Thank You!
>
>
>
>
> file_list <- list.files()
>   
> for (file in file_list){
>         
>   
>    if (!exists("dataset")){
>      dataset <- read.table(file, header=TRUE, sep="\t")
>    }
>     
>   
>    if (exists("dataset")){
>      temp_dataset <-read.table(file, header=TRUE, sep="\t")
>
>
>   ######how  to plot(temp_dataset) each file and save png(file\.png)
>   
>    }
>   
> }
>
> ______________________________________________
> 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.



More information about the R-help mailing list