Thomas Barningham
2014-Dec-05 13:40 UTC
[R] Run script automatically on several input files in the directory and produce separate outputs?
Hi, I have written a script that currently reads in a .txt file where I specify the name e.g mydata<-read.table("a_date.txt", header=TRUE) The script eventually produces a plot, e.g: pdf(file="myfilename.txt") plot(etc) dev.off What I want to do is run this script on several input files in my directory, without having to manually change the input file name each time, and produce the output plot pdf with the input file name as the output file name. It would also be handy if my plot title is also the input file name. I'm relatively new to R so i'm not sure how to approach this. I presume it's some sort of loop function, but i've never implemented one of these before - any advice would be greatly appreciated! Thanks in advance! Thomas -- Thomas Barningham Centre for Ocean and Atmospheric Sciences School of Environmental Sciences University of East Anglia Norwich Research Park Norwich NR4 7TJ
ONKELINX, Thierry
2014-Dec-05 14:20 UTC
[R] Run script automatically on several input files in the directory and produce separate outputs?
Dear Thomas, list.files() will be your new best friend. Best regards, ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: R-help [mailto:r-help-bounces at r-project.org] Namens Thomas Barningham Verzonden: vrijdag 5 december 2014 14:41 Aan: r-help at r-project.org Onderwerp: [R] Run script automatically on several input files in the directory and produce separate outputs? Hi, I have written a script that currently reads in a .txt file where I specify the name e.g mydata<-read.table("a_date.txt", header=TRUE) The script eventually produces a plot, e.g: pdf(file="myfilename.txt") plot(etc) dev.off What I want to do is run this script on several input files in my directory, without having to manually change the input file name each time, and produce the output plot pdf with the input file name as the output file name. It would also be handy if my plot title is also the input file name. I'm relatively new to R so i'm not sure how to approach this. I presume it's some sort of loop function, but i've never implemented one of these before - any advice would be greatly appreciated! Thanks in advance! Thomas -- Thomas Barningham Centre for Ocean and Atmospheric Sciences School of Environmental Sciences University of East Anglia Norwich Research Park Norwich NR4 7TJ ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. **** D I S C L A I M E R **** Bezoek onze website/Visit our website<https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo>
Thomas Barningham
2014-Dec-05 15:00 UTC
[R] Run script automatically on several input files in the directory and produce separate outputs?
Dear Thierry, Thanks for your suggestion...but I don't how I would apply this for my situation, the R help isn't much help for me either. (Apologies - I am a rookie!) Do I still need a for loop? Many thanks Thomas On Fri, Dec 5, 2014 at 2:20 PM, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:> Dear Thomas, > > list.files() will be your new best friend. > > Best regards, > > ir. Thierry Onkelinx > Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance > Kliniekstraat 25 > 1070 Anderlecht > Belgium > + 32 2 525 02 51 > + 32 54 43 61 85 > Thierry.Onkelinx at inbo.be > www.inbo.be > > To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. > ~ Sir Ronald Aylmer Fisher > > The plural of anecdote is not data. > ~ Roger Brinner > > The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. > ~ John Tukey > > -----Oorspronkelijk bericht----- > Van: R-help [mailto:r-help-bounces at r-project.org] Namens Thomas Barningham > Verzonden: vrijdag 5 december 2014 14:41 > Aan: r-help at r-project.org > Onderwerp: [R] Run script automatically on several input files in the directory and produce separate outputs? > > Hi, > > I have written a script that currently reads in a .txt file where I specify the name e.g > > mydata<-read.table("a_date.txt", header=TRUE) > > The script eventually produces a plot, e.g: > > pdf(file="myfilename.txt") > plot(etc) > dev.off > > What I want to do is run this script on several input files in my directory, without having to manually change the input file name each time, and produce the output plot pdf with the input file name as the output file name. It would also be handy if my plot title is also the input file name. > > I'm relatively new to R so i'm not sure how to approach this. I presume it's some sort of loop function, but i've never implemented one of these before - any advice would be greatly appreciated! > > Thanks in advance! > Thomas > -- > Thomas Barningham > Centre for Ocean and Atmospheric Sciences School of Environmental Sciences University of East Anglia Norwich Research Park Norwich > NR4 7TJ > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. > **** D I S C L A I M E R **** > Bezoek onze website/Visit our website<https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo>-- Thomas Barningham Centre for Ocean and Atmospheric Sciences School of Environmental Sciences University of East Anglia Norwich Research Park Norwich NR4 7TJ
Jeff Newmiller
2014-Dec-05 15:10 UTC
[R] Run script automatically on several input files in the directory and produce separate outputs?
Of course, you probably also need to wrap your code into a function first. R script files are not very good units of code to repeat multiple times. Once you have a function that you can give a data file name to and get a plot, then it is easy to use the lapply function to call that function once for each filename you get back from the list.files function. Reading other emails on this list can be helpful also. In the last day there was a discussion about how to rename files which could give you ideas how to automatically make new filenames to write your plot images into. See the archives mentioned in the link in the footer of this message. E.g. myplotfilecreator <- function( fname ) { pngfname <- paste0( fname, ".png" ) # code to create png file pngfname # return output file name when done } inputdir <- "data" # where your input files are fnames <- list.files( inputdir, full.names=TRUE ) lapply( fnames, myplotfilecreator ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On December 5, 2014 6:20:47 AM PST, "ONKELINX, Thierry" <Thierry.ONKELINX at inbo.be> wrote:>Dear Thomas, > >list.files() will be your new best friend. > >Best regards, > >ir. Thierry Onkelinx >Instituut voor natuur- en bosonderzoek / Research Institute for Nature >and Forest >team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance >Kliniekstraat 25 >1070 Anderlecht >Belgium >+ 32 2 525 02 51 >+ 32 54 43 61 85 >Thierry.Onkelinx at inbo.be >www.inbo.be > >To call in the statistician after the experiment is done may be no more >than asking him to perform a post-mortem examination: he may be able to >say what the experiment died of. >~ Sir Ronald Aylmer Fisher > >The plural of anecdote is not data. >~ Roger Brinner > >The combination of some data and an aching desire for an answer does >not ensure that a reasonable answer can be extracted from a given body >of data. >~ John Tukey > >-----Oorspronkelijk bericht----- >Van: R-help [mailto:r-help-bounces at r-project.org] Namens Thomas >Barningham >Verzonden: vrijdag 5 december 2014 14:41 >Aan: r-help at r-project.org >Onderwerp: [R] Run script automatically on several input files in the >directory and produce separate outputs? > >Hi, > >I have written a script that currently reads in a .txt file where I >specify the name e.g > >mydata<-read.table("a_date.txt", header=TRUE) > >The script eventually produces a plot, e.g: > >pdf(file="myfilename.txt") >plot(etc) >dev.off > >What I want to do is run this script on several input files in my >directory, without having to manually change the input file name each >time, and produce the output plot pdf with the input file name as the >output file name. It would also be handy if my plot title is also the >input file name. > >I'm relatively new to R so i'm not sure how to approach this. I presume >it's some sort of loop function, but i've never implemented one of >these before - any advice would be greatly appreciated! > >Thanks in advance! >Thomas >-- >Thomas Barningham >Centre for Ocean and Atmospheric Sciences School of Environmental >Sciences University of East Anglia Norwich Research Park Norwich >NR4 7TJ > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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. >**** D I S C L A I M E R **** >Bezoek onze website/Visit our >website<https://drupal.inbo.be/nl/disclaimer-mailberichten-van-het-inbo> > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
MacQueen, Don
2014-Dec-05 19:07 UTC
[R] Run script automatically on several input files in the directory and produce separate outputs?
The simplest approach, and a good one for someone new to R, would be something like this: myfiles <- c('fileA', 'fileB','fileC") for (nm in myfiles) { cat('now reading input file',nm,'\n') mydat <- read.table( paste0(nm,'.txt'), header=TRUE) pdf( paste0(nm, '.pdf') ) plot(etc , main=nm) dev.off() } I stored the file names without the ".txt", so that the base part of the name could more easily be used to construct the pdf file name. The paste0() command is used to construct the full input and pdf file names, i.e., with the .txt and .pdf suffices. Fancier tools include using functions like list.files() to, for example, find the names of all the .txt files in your current working directory, but until you understand what I've shown you, I wouldn't tackle those. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 12/5/14, 5:40 AM, "Thomas Barningham" <stbarningham at gmail.com> wrote:>Hi, > >I have written a script that currently reads in a .txt file where I >specify the name e.g > >mydata<-read.table("a_date.txt", header=TRUE) > >The script eventually produces a plot, e.g: > >pdf(file="myfilename.txt") >plot(etc) >dev.off > >What I want to do is run this script on several input files in my >directory, without having to manually change the input file name each >time, and produce the output plot pdf with the input file name as the >output file name. It would also be handy if my plot title is also the >input file name. > >I'm relatively new to R so i'm not sure how to approach this. I >presume it's some sort of loop function, but i've never implemented >one of these before - any advice would be greatly appreciated! > >Thanks in advance! >Thomas >-- >Thomas Barningham >Centre for Ocean and Atmospheric Sciences >School of Environmental Sciences >University of East Anglia >Norwich Research Park >Norwich >NR4 7TJ > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.