ppaarrkk
2009-Jan-13 11:53 UTC
[R] Re ad a text file from a directory in which an R script finds itself
Is it possible for an R script to read a text file, say, from the directory in which the script is located ? I don't think I can use setwd(), because I can't specify the directory. -- View this message in context: http://www.nabble.com/Read-a-text-file-from-a-directory-in-which-an-R-script-finds-itself-tp21434236p21434236.html Sent from the R help mailing list archive at Nabble.com.
Rau, Roland
2009-Jan-13 12:00 UTC
[R] Re ad a text file from a directory in which an R script finds itself
Hi,> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of ppaarrkk > Sent: Tuesday, January 13, 2009 12:53 PM > To: r-help at r-project.org > Subject: [R] Re ad a text file from a directory in which an R > script finds itself > > > Is it possible for an R script to read a text file, say, from > the directory > in which the script is located ? > > I don't think I can use setwd(), because I can't specify the > directory.maybe you have to be a bit more specific? What do you mean by "I can't specify..."? - you don't know how to specify the directory -> setwd("c:/mydirectory/anotherdirectoy/") mydata <- read.table(.....) - you don't know the directory...hmmm, how do you know then that the script file is there? Does this help? Roland ---------- This mail has been sent through the MPI for Demographic Research. Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.
Gabor Grothendieck
2009-Jan-13 12:07 UTC
[R] Re ad a text file from a directory in which an R script finds itself
This is an ugly hack but add this line to your sourced script: this.dir <- dirname(parent.frame(2)$ofile) It must be at top level, i.e. not in a function in the script. On Tue, Jan 13, 2009 at 6:53 AM, ppaarrkk <simon_ecc at yahoo.co.uk> wrote:> > Is it possible for an R script to read a text file, say, from the directory > in which the script is located ? > > I don't think I can use setwd(), because I can't specify the directory. > -- > View this message in context: http://www.nabble.com/Read-a-text-file-from-a-directory-in-which-an-R-script-finds-itself-tp21434236p21434236.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >