[R] R scripts and parameters

2009-02-17 Thread mauede
A couple of weeks ago I asked how it is possible to run an R script (not a 
function) passing some parameters.
Someone suggested the function "commandArgs()".
I read the on-line help and found no clarifying example. Therefore I do not 
know how to use it appropriately.
I noticed this function returns the pathname of the R executable which is not 
what I need.

I meant to ask if it is possible to open an R session and launch a script 
passing parameters that the script can retrieve and use itself.
Just like in C I can run a program and call it with  some arguments 

> Example_Prog A B C
  
The program "Example_Prog" can acess its own arguments through the data 
structures "argc" an "argv".

How can I launch an R script simulating the above mechanism ? 
Shall I use source ("script-name") ?
Where are the arguments to be passed, as part of the source call ?
Is the function "commandArgs" to be places as one of the first code lines of 
the script in order to access its own arguments ?
Is there any "commandArgs" usage example ?

Thank you very much in advance.
Maura






tutti i telefonini TIM!


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] R scripts and parameters

2009-02-17 Thread Gabor Grothendieck
Try this:

A <- 1
B <- 2
C <- 3
source("myfile.R")

Now the code in myfile can access A, B and C.

On Tue, Feb 17, 2009 at 10:55 AM,   wrote:
> A couple of weeks ago I asked how it is possible to run an R script (not a 
> function) passing some parameters.
> Someone suggested the function "commandArgs()".
> I read the on-line help and found no clarifying example. Therefore I do not 
> know how to use it appropriately.
> I noticed this function returns the pathname of the R executable which is not 
> what I need.
>
> I meant to ask if it is possible to open an R session and launch a script 
> passing parameters that the script can retrieve and use itself.
> Just like in C I can run a program and call it with  some arguments
>
>> Example_Prog A B C
>
> The program "Example_Prog" can acess its own arguments through the data 
> structures "argc" an "argv".
>
> How can I launch an R script simulating the above mechanism ?
> Shall I use source ("script-name") ?
> Where are the arguments to be passed, as part of the source call ?
> Is the function "commandArgs" to be places as one of the first code lines of 
> the script in order to access its own arguments ?
> Is there any "commandArgs" usage example ?
>
> Thank you very much in advance.
> Maura
>
>
>
>
>
>
> tutti i telefonini TIM!
>
>
>[[alternative HTML version deleted]]
>
> __
> R-help@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.
>

__
R-help@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.


Re: [R] R scripts and parameters

2009-02-17 Thread Duncan Murdoch

On 2/17/2009 10:55 AM, mau...@alice.it wrote:

A couple of weeks ago I asked how it is possible to run an R script (not a 
function) passing some parameters.
Someone suggested the function "commandArgs()".
I read the on-line help and found no clarifying example. Therefore I do not 
know how to use it appropriately.
I noticed this function returns the pathname of the R executable which is not 
what I need.

I meant to ask if it is possible to open an R session and launch a script 
passing parameters that the script can retrieve and use itself.
Just like in C I can run a program and call it with  some arguments 


Example_Prog A B C
  
The program "Example_Prog" can acess its own arguments through the data structures "argc" an "argv".


How can I launch an R script simulating the above mechanism ? 
Shall I use source ("script-name") ?

Where are the arguments to be passed, as part of the source call ?
Is the function "commandArgs" to be places as one of the first code lines of 
the script in order to access its own arguments ?
Is there any "commandArgs" usage example ?



Gabor gave you a solution from within R.  If you want to run a script 
from the command line, then use commandArgs(TRUE).  For example, put 
this into the file test.R:


commandArgs(TRUE)

(The TRUE says you only want to see the trailing arguments, not 
everything else on the command line.)


Then from the command line, do

Rscript test.R A B C

and you'll see the output

[1] "A" "B" "C"

Duncan Murdoch

__
R-help@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.


[R] R: R scripts and parameters

2009-02-18 Thread mauede
Sorry. This is still unclear to me.
I generated a file called "Test.R" that contains the following lines:

commandArgs(TRUE)
cat("\n A = ",A,"\n")
cat("\n B = ",B,"\n")
cat("\n C = ",C,"\n")

First of all I have to clarify which command line we are talking about.
If I run the command "Rscript" from a Windows terminal, the system does not 
recognize such a command:
C:\Documents and Settings\Monville\Utilities-Dir>Rscript Test.R aa bb cc
"Rscript" non รจ riconosciuto come comando interno o esterno,
 un programma eseguibile o un file batch.
The above system response tells me that Rscript is not recognized either as an 
internal or external command, an executable or a batch file.

In fact Rscript is an R command. Nevertheless, I started an R session and tried 
such a command from R console command line
and got the following:
> getwd()
[1] "C:/Documents and Settings/Monville/Utilities-Dir"
> Rscript Test.R aa bb cc
Error: unexpected symbol in "Rscript Test.R"

I feel I do not have a good grasp of how to run R scripts the same way as I 
usually run C programs.
Any help is welcome. 
Thank  you.

Maura 





-Messaggio originale-
Da: Duncan Murdoch [mailto:murd...@stats.uwo.ca]
Inviato: mar 17/02/2009 17.34
A: mau...@alice.it
Cc: r-h...@stat.math.ethz.ch
Oggetto: Re: [R] R scripts and parameters
 
On 2/17/2009 10:55 AM, mau...@alice.it wrote:
> A couple of weeks ago I asked how it is possible to run an R script (not a 
> function) passing some parameters.
> Someone suggested the function "commandArgs()".
> I read the on-line help and found no clarifying example. Therefore I do not 
> know how to use it appropriately.
> I noticed this function returns the pathname of the R executable which is not 
> what I need.
> 
> I meant to ask if it is possible to open an R session and launch a script 
> passing parameters that the script can retrieve and use itself.
> Just like in C I can run a program and call it with  some arguments 
> 
>> Example_Prog A B C
>   
> The program "Example_Prog" can acess its own arguments through the data 
> structures "argc" an "argv".
> 
> How can I launch an R script simulating the above mechanism ? 
> Shall I use source ("script-name") ?
> Where are the arguments to be passed, as part of the source call ?
> Is the function "commandArgs" to be places as one of the first code lines of 
> the script in order to access its own arguments ?
> Is there any "commandArgs" usage example ?


Gabor gave you a solution from within R.  If you want to run a script 
from the command line, then use commandArgs(TRUE).  For example, put 
this into the file test.R:

commandArgs(TRUE)

(The TRUE says you only want to see the trailing arguments, not 
everything else on the command line.)

Then from the command line, do

Rscript test.R A B C

and you'll see the output

[1] "A" "B" "C"

Duncan Murdoch




tutti i telefonini TIM!


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] R scripts and parameters

2009-02-20 Thread Jeffrey J. Hallman
Here's how I do this:

The last lines of my .First() function are these:

## Run command line program if there is one
if(length(.cmd <- commandArgs(trailingOnly = TRUE)) > 0)
try(source(textConnection(.cmd), echo = T, prompt.echo = "> "))

and on my Linux path I have this csh script, called "runR"

#! /bin/csh
# This is the runR script.
# It starts R in a specified directory (default /mra/prod/R)
# and submits the string given as its last argument to that R process.
# To submit multiple Rcommands, use a single string with the commands 
# separated by semi-colons.  
#
set usage = 'runR [-d directory] "cmd1; cmd2; cmd3"'
#
#  Example: runR "chartControl()"
#
#starts R and immediately invokes the chartControl()
#function, and exits when chartControl() does.

if ($#argv == 0 ) then
  echo $usage
  exit(1)
endif

set rDir = /mra/prod/R
if("$1" == "-d") then
  shift
  set directory = $1
  shift
else
  set directory = $rDir
endif

umask 002
cd $directory

set log = $rDir/tmp/runR.log
if( -e $log ) then
  set nLines = `wc -l $log | awk '{print $1}'`
  if($nLines > 25000) then
tail -2 $log > $log.tmp
mv $log.tmp $log
  endif
endif

R --no-save --args "$argv ; q()" |& tee -a $log
# end of runR script


-- 
Jeff

__
R-help@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.


Re: [R] R: R scripts and parameters

2009-02-19 Thread Prof Brian Ripley
You seem not to have put the R bin directory in your path.  That is 
where Rterm, Rscript ... are installed (and the installer does not 
change the PATH for you).


On Thu, 19 Feb 2009, mau...@alice.it wrote:


Sorry. This is still unclear to me.
I generated a file called "Test.R" that contains the following lines:

commandArgs(TRUE)
cat("\n A = ",A,"\n")
cat("\n B = ",B,"\n")
cat("\n C = ",C,"\n")

First of all I have to clarify which command line we are talking about.
If I run the command "Rscript" from a Windows terminal, the system does not 
recognize such a command:
C:\Documents and Settings\Monville\Utilities-Dir>Rscript Test.R aa bb cc
"Rscript" non ? riconosciuto come comando interno o esterno,
un programma eseguibile o un file batch.
The above system response tells me that Rscript is not recognized either as an 
internal or external command, an executable or a batch file.

In fact Rscript is an R command. Nevertheless, I started an R session and tried 
such a command from R console command line
and got the following:

getwd()

[1] "C:/Documents and Settings/Monville/Utilities-Dir"

Rscript Test.R aa bb cc

Error: unexpected symbol in "Rscript Test.R"

I feel I do not have a good grasp of how to run R scripts the same way as I 
usually run C programs.
Any help is welcome.
Thank  you.

Maura





-Messaggio originale-
Da: Duncan Murdoch [mailto:murd...@stats.uwo.ca]
Inviato: mar 17/02/2009 17.34
A: mau...@alice.it
Cc: r-h...@stat.math.ethz.ch
Oggetto: Re: [R] R scripts and parameters

On 2/17/2009 10:55 AM, mau...@alice.it wrote:

A couple of weeks ago I asked how it is possible to run an R script (not a 
function) passing some parameters.
Someone suggested the function "commandArgs()".
I read the on-line help and found no clarifying example. Therefore I do not 
know how to use it appropriately.
I noticed this function returns the pathname of the R executable which is not 
what I need.

I meant to ask if it is possible to open an R session and launch a script 
passing parameters that the script can retrieve and use itself.
Just like in C I can run a program and call it with  some arguments


Example_Prog A B C


The program "Example_Prog" can acess its own arguments through the data structures "argc" 
an "argv".

How can I launch an R script simulating the above mechanism ?
Shall I use source ("script-name") ?
Where are the arguments to be passed, as part of the source call ?
Is the function "commandArgs" to be places as one of the first code lines of 
the script in order to access its own arguments ?
Is there any "commandArgs" usage example ?



Gabor gave you a solution from within R.  If you want to run a script
from the command line, then use commandArgs(TRUE).  For example, put
this into the file test.R:

commandArgs(TRUE)

(The TRUE says you only want to see the trailing arguments, not
everything else on the command line.)

Then from the command line, do

Rscript test.R A B C

and you'll see the output

[1] "A" "B" "C"

Duncan Murdoch




tutti i telefonini TIM!


[[alternative HTML version deleted]]




--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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.