Skip to content

filename of source file

4 messages · David Richmond, Andrew C. Ward, Jim Lemon +1 more

#
Yes, source refers to the use of the source() function. Suppose I invoke 
a file like so:

source("/path/name/filename.r")

I want to insert a command in filename.r like so:

fullpath <- magic_command()

such that fullpath is now "/path/name/filename.r"

In other words, I want filename.r to be able to figure out where it is, 
regardless of where I put it, so that it can save all its output in the 
same directory.

dave r
On Sunday, August 4, 2002, at 10:09 PM, Andrew C. Ward wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
If you use the chdir argument to source(), this will tell your .r file to
work in that directory (including writing output to files). For example,
if you have a file called test.r in c:/tmp that contains the commands
        my.test <- sample(1:5, 100, replace=TRUE)
        write.table(my.test, "test.out")
you can call it using
        > source("c:/temp/test.r", chdir=TRUE)
after which there will be a file in c:/temp called test.out.

Does this achieve what you want?


Regards,

Andrew C. Ward

CAPE Centre,
The University of Queensland
Brisbane Qld 4072 Australia
andreww at cheque.uq.edu.au

----- Original Message -----
From: David Richmond <daver969 at mac.com>
To: Andrew C. Ward <andreww at cheque.uq.edu.au>
Cc: <r-help at hypatia.math.ethz.ch>
Sent: Monday, August 05, 2002 3:25 PM
Subject: Re: [R] filename of source file
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
David Richmond wrote:
How about

source(fullpath<-"/path/name/filename.r") ?

Jim
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 5 Aug 2002, Jim Lemon wrote:

            
That will work only when run at the top level.

If it isn't sufficient to know the directory

 	source("/path/name/filename.r",chdir=TRUE)

in which case magic.command() is getwd(), there isn't a nice way to do it.

Two not-so-nice ways

1) modify source() so it does something like

    assign(".Last.source.file",file,envir=.GlobalEnv)

   giving
    magic.command<-function() get(".Last.source.file",envir=.GlobalEnv)


2) Get the file name from the environment inside source

     magic.command<-function(){
	n<-match("source",rev(as.character(sapply(sys.calls(),"[[",1))))
	get("file", parent.frame(n-1)
	}



The problem with the first is that it writes in the global workspace and
that it fails if a source()d file source()s another file before you use
magic.command().

The problem with the second is that it will break if the source() function is
renamed or if the name of the variable "file" changes.


You also need to be aware that `file' need not be a filename. It could be
a URL [eg source("http://www.bioconductor.org/getBioC.R") ] or a
connection (either a file connection or some other sort).


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._