Skip to content

scan not working

6 messages · Peter Dalgaard, Steve Lianoglou, Rui Barradas +1 more

#
Hi all, 

I am trying to use the scan function in an R script that I am calling from the command line on a Mac; at the shell prompt I type: 

$ Rscript get_q_values.R LRT_codeml_output 

in the hope that LRT_codeml_output will get passed to the get_q_values R script. The first line of that script is: 

chidata <- scan(file="")

which, as I understand how scan works, will read the contents of the file from the command line into the object chidata. I did this a few times and it worked like a charm. And then, it stopped working. Now, every time I try to do this, I get "Read 0 items" as the next line in the terminal window, and the output produced by the script is empty, because it's apparently no longer reading anything in. I don't think I changed anything in the script; it just stopped being able to execute the scan function. Does anyone have any idea how to fix this?? I did not have anything else in that scan line when it was working before. I've updated R and restarted my computer in the hope that it would help, but it hasn't. Any help would be much appreciated.

-ES
#
On Jan 27, 2013, at 08:33 , Emily Sessa wrote:

            
I don't see how that would ever work. The 2nd and further args to Rscript are passed to R and accesible via commandArgs(). There's no way that scan() can know what the arguments are. It might work with 

Rscript get_q_values.R < LRT_codeml_output

though. Or you need to arrange explicitly for scan(file=commandArgs(TRUE)[1]).

  
    
#
Hi,

It sounds like you just want to write a command line script using R
and you would pass the suffix/prefix as command line args, no?

Why not just go with what Peter has already suggested with
`commandArgs`, or if you want a more feature-rich command line arg
parser, you can try:

http://cran.r-project.org/web/packages/optparse/index.html

If the command line argument thing won't work for you, perhaps you can
elaborate more? For instance, you mention that you know how you might
do "this" in Perl ... perhaps you can clarify "this" a bit more.

-steve
On Sun, Jan 27, 2013 at 12:27 PM, Emily Sessa <sessa.emily at gmail.com> wrote:

  
    
#
Hello,

Try the following. Create a file called test.R with these instructions:

cmd <- commandArgs(TRUE)
if(any(grepl("--ext", cmd))){
	suffix <- cmd[grep("--ext", cmd)]
	suffix <- unlist(strsplit(suffix, ":"))[2]
	fl <- paste0("test_", suffix, ".txt") # underscore included
}else{
	fl <- "test.txt"
}
write(cmd, file = fl)

# Call like this
#$ Rscript test.R other --options --ext:cro


This creates a file test_cro.txt with the command line options written 
to it. You can adapt this R script to your needs.
Instead of 'suf', I've used option '--ext' for 'extension'. Change to 
fit your taste.


Hope this helps,

Rui Barradas

Em 27-01-2013 17:27, Emily Sessa escreveu:
#
Thank you all - these suggestions have been very helpful! I've got it doing what I wanted know, and I appreciate the help!

Emily
On Jan 27, 2013, at 1:18 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote: