Please see also ?commandArgs

Uwe Ligges


Darren Weber wrote:

---------- Forwarded message ----------
From: Darren Weber <[EMAIL PROTECTED]>
Date: Apr 8, 2005 1:16 PM
Subject: Re: [R] Is a .R script file name available inside the script?
To: Gabor Grothendieck <[EMAIL PROTECTED]>

Right, I understand, it could be in.txt has:

scriptfile <- "Rscript.R"
source(scriptfile)

and then Rscript.R contains:

script.description <- function() eval.parent(quote(scriptfile), n = 3)
print(basename(script.description()))

Actually, I've found that just 'scriptfile <-
basename(eval.parent(quote(scriptfile))'
is sufficient. Unfortunately, this now requires two files, in.txt AND Rscript.R!


I think this is a bit clumsy. You need a batch file to define a variable in the 'parent' workspace that is then referred to in the source(script) file. I really just want the argv like construct to work, but having read a few other threads on that topic, it seems that the file name argument is stripped from argv, so this raises a whole lot of trouble to get that working (such as setting an environment variable and then using glob on os.getenv). Anyhow, it's not the elegant, cross-platform solution I expect and desire of R, but there are some solutions.

I would suggest that R implements a command line argument, maybe --infile and then that filename is available as an internal variable in a standard argv like fashion. The default sink output should be stdout, but a nice alternative is to replace the .R extension with .Rout. Then it becomes possible to call R on any platform with

R --vanilla --infile Rscript.R

instead of

R --vanilla < Rscript.R
or
cat Rscript.R | R --vanilla

On linux, the output would go to stdout/stderr and on windows it might go into Rscript.Rout Have I missed a command line input argument like that? Does --infile exist?

BTW, I think you meant linux above in reference to the command, echo "source(Rscript.R)" | R --vanilla, as I'm not aware of pipes on windows.

Best, Darren






On Apr 6, 2005 6:56 PM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:

It works for me. Suppose in.txt is a two line file with these two lines:

file <- "Rscript.R"
source(file)

and Rscript.R is a two line file with these two lines:

script.description <- function() eval.parent(quote(file), n = 3)
print(basename(script.description()))

Then here is the output on Windows:

C:\Program Files\R\rw2001beta\bin>R --vanilla < in.txt

R : Copyright 2004, The R Foundation for Statistical Computing
[snip]

file <- "Rscript.R"
source(file)

[1] "Rscript.R"

Note that 'file' referred to in 'eval.parent' is not the variable that
you called 'file' but is an internal variable within the 'source'
program that is called 'file'. It has nothing to do with your 'file',
which very well could have a different name. In fact you
just do this on Windows:

echo source("Rscript.R") | R --vanilla

From: Darren Weber <[EMAIL PROTECTED]>

That is useful, when calling the script like this:


file <- "Rscript.R"
source(file)

However, it does not work if we do this from the shell prompt:

$ R --vanilla < Rscript.R

because the eval.parent statement attempts to access a "base
workspace"that does not contain the "file" object/variable, as above.
Isthere a solution for this situation? Is the input script file
anargument to R and therefore available in something like argv?

On Mar 18, 2005 8:00 PM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote:
Darren Weber <darrenleeweber <at> gmail.com <http://gmail.com>> writes:


:
: Hi,
:
: if we have a file called Rscript.R that contains the following, for example:
:
: x <- 1:100
: outfile = "Rscript.Rout"
: sink(outfile)
: print(x)
:
: and then we run
:
: >> source("Rscript.R")
:
: we get an output file called Rscript.Rout - great!
:
: Is there an internal variable, something like .Platform, that holds
: the script name when it is being executed? I would like to use that
: variable to define the output file name.
:


In R 2.0.1 try putting this in a file and sourcing it.

script.description <- function() eval.parent(quote(file), n = 3)
print(basename(script.description()))

If you are using R 2.1.0 (devel) then use this instead:

script.description <- function()
showConnections() [as.character(eval.parent(quote(file), n = 3)),
"description"]
print((basename(script.description())))

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




        [[alternative HTML version deleted]]

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to