[R] scan not working
ruipbarradas at sapo.pt
Sun Jan 27 21:18:12 CET 2013
Try the following. Create a file called test.R with these instructions:
cmd <- commandArgs(TRUE)
suffix <- cmd[grep("--ext", cmd)]
suffix <- unlist(strsplit(suffix, ":"))
fl <- paste0("test_", suffix, ".txt") # underscore included
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,
Em 27-01-2013 17:27, Emily Sessa escreveu:
> Hello all (again),
> I received a very helpful answer to this question, and would like to pose one more:
> Right now I have this script, which is being called from the command line, writing output to two generically named files ("pvalues" and "qvalues") that are named in the script using the line:
> write(pvalues, file="pvalues", ncol=1)
> write(adjusted, file="qvalues", ncol=1)
> However, ideally I would like those two files to have something appended to their names that make them separate from one another, so I can identify which input they went with and so they won't write over each other when I script this into a Perl pipeline that will process many input files, which is my ultimate goal. I know how to do this in Perl, but not R... is there some way I can add another argument on the command line that will get passed to the R.script, like a simple letter code (e.g. "cro"), and then have it append that to the output file names, so they are, for example: "qvalues_cro" and "pvalues_cro"?
> Thank you very much,
> On Jan 27, 2013, at 4:34 AM, peter dalgaard <pdalgd at gmail.com> wrote:
>> On Jan 27, 2013, at 08:33 , Emily Sessa wrote:
>>> 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.
>> 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)).
>>> R-help at r-project.org mailing list
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help