Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

stopping the script until a value is entred from keyboard in R

i have a script , and i want to start it with a keyboard input and then continue the work based on the value entered in this variable , i tried y=readline("please enter a value") but the script won't wait to enter a value , it just display this sentence and continue , how this can be done ??? thanks in advance ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a very simple #! script that creates a function foo() whose sole purpose is to echo back out it's argument 'bar'.

#! /home/gavin/R/2.13-patched/build/bin/Rscript --vanilla
foo <- function(bar) {
    writeLines(paste("You supplied the following information:", bar))
}

## grab command arguments passed as -args
args <- commandArgs(TRUE)

## run foo
foo(args)

We grab any command line arguments passed to the script from the shell using the commandArgs() function, and then pass them to foo() with the last line of the script.

If we have that bit of code in file foobar.R, then we can pass in an argument and run the script using the Rscript interface. You also need to make the above executable (chmod it).

Then the script can be called like and works as follows:

[gavin@desktop ~]$ ./foobar.R Cl
You supplied the following information: Cl

But do note the information in ?Rscript as unfortunately, standard Windows cmd shells don't know about #! like scripts, so you might need to install some other shell (the help suggests a Cygwin shell should work) to use the functionality I show.

Update: Using source() and readline().

An alternative, if you can do without having to run non-interactively (i.e. you don't min opening the R GUI and running one line of code), is to just source() the script. For example, if this was in a script call barfoo.R:

dynamicwilcox <- function() {
    ANSWER <- readline("What column do you want to work on? ")
    if(ANSWER=="Ph") {
        writeLines("column was 'Ph'")
    } else if(ANSWER=="Cl") {
        writeLines("column was 'Cl'")
    } else {
        writeLines(paste("Sorry, we don't know what to do with column", ANSWER))
    }
    ANSWER ## return something
}

dynamicwilcox()

Then from the R Gui prompt, we could do:

R> source("barfoo.R")
What column do you want to work on? Cl
column was 'Cl'

or if you don't want to specify the full path do this:

R> source(file.choose())

readline() works just fine when used in an interactive R session and is really the best tool for the job - this is exactly what it was deigned to do.

The whole premise that you want to run a script in a batch mode but provide some input doesn't make much sense. R expects scripts to be self contained when run in batch mode. You might not realise it, but when you double click your script, it is being run in batch mode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...