Suppose I create an R program called myTest.R with only one line like the following: type <- as.integer(readline("input type (1: type1; 2: type2)? ")) Then I'd like to run myTest.R in batch mode by constructing an input file called answers.R with the following: source("myTest.R") 1 When I ran the following at the terminal: R CMD BATCH answer.R output.Rout it failed to pick up the answer '1' from the 2nd line in answers.R as shown inside output.Rout:> source("myTest.R")input type (0: quit; 1: type1; 2: type2)?> 1[1] 1 What am I missing here? Thanks in advance, Gang
You're not missing anything. In your output.Rout: the ">1" right after the source('test') is the "1" inputed from answers.R. the "[1] 1" is the result of test. Remove the second line from answers.R and see what happens (hint: script ends after the readline prompt). Just out of curiosity, why will you use a script that requires user input (readlines) in batch mode ? Cheers On Tue, Feb 7, 2012 at 4:05 PM, Gang Chen <gangchen6 at gmail.com> wrote:> Suppose I create an R program called myTest.R with only one line like > the following: > > type <- as.integer(readline("input type (1: type1; 2: type2)? ")) > > Then I'd like to run myTest.R in batch mode by constructing an input > file called answers.R with the following: > > source("myTest.R") > 1 > > When I ran the following at the terminal: > > R CMD BATCH answer.R output.Rout > > it failed to pick up the answer '1' from the 2nd line in answers.R as > shown inside output.Rout: > >> source("myTest.R") > input type (0: quit; 1: type1; 2: type2)? >> 1 > [1] 1 > > What am I missing here? > > Thanks in advance, > Gang > > ______________________________________________ > R-help at 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.
On Tue, Feb 7, 2012 at 10:50 PM, ilai <keren at math.montana.edu> wrote:> Ahh, > I think I'm getting it now. Well, readlines() is not going to work for > you. The help file ?readline clearly states "In non-interactive use > the result is as if the response was RETURN and the value is ?""?." > The implication is you cannot use it to "insert" different answers as > if you were really there. > How about using eval() instead? You will need to make the answers a > named list (or just assigned objects). > > test <- expression({ > ?if(a>2) print('+') > ?else print('I got more') > ?b <- b+3 ? # reassign b in the environment > ?print(b) > ?print(c) > ?d^2 > }) > dump('test',file='myTest.R') ; rm(test) > > # make the answers.R file: > > a=5 ; b=2 ; c=2 ; d=3 > source("myTest.R") > eval(test) > > # Now, from the terminal ?R CMD BATCH answers.R out.R > # And here is my $ cat out.R > ... flushed >> a=5 ; b=2 ; c=2 ; d=3 >> source("myTest.R") >> eval(test) > [1] "+" > [1] 5 > [1] 2 > [1] 9 >> >> proc.time() > ? user ?system elapsed > ?0.640 ? 0.048 ? 0.720 > > Would this work? > Elai > > > > > On Tue, Feb 7, 2012 at 4:05 PM, Gang Chen <gangchen6 at gmail.com> wrote: >> Suppose I create an R program called myTest.R with only one line like >> the following: >> >> type <- as.integer(readline("input type (1: type1; 2: type2)? ")) >> >> Then I'd like to run myTest.R in batch mode by constructing an input >> file called answers.R with the following: >> >> source("myTest.R") >> 1 >> >> When I ran the following at the terminal: >> >> R CMD BATCH answer.R output.Rout >> >> it failed to pick up the answer '1' from the 2nd line in answers.R as >> shown inside output.Rout: >> >>> source("myTest.R") >> input type (0: quit; 1: type1; 2: type2)? >>> 1 >> [1] 1 >> >> What am I missing here? >> >> Thanks in advance, >> Gang >> >> ______________________________________________ >> R-help at 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.
Gang, Maybe someone here has a different take on things. I'm afraid I have no more insights on this unless you explain exactly what you are trying to achieve, or more importantly why? That may help understand what the problem really is. Do you want to save an interactive session for future runs? then ?save.image and all your "answers" are in the environment. In this case consider putting an "if(!exists('type') | length(type)<1 | is.na(type))" before "type<- readline(...)" in your script so type wouldn't be overwritten in subsequent runs. If your goal is to batch evaluate multiple answer files from users (why else would you ask questions with readline?), then you should have enough to go on with my answer and the examples in ?eval. Elai On Wed, Feb 8, 2012 at 9:04 AM, Gang Chen <gangchen6 at gmail.com> wrote:> Hi Elai, > > Thanks a lot for the suggestions! I really appreciate it... > > Your suggestion of using eval() and creating those answers in a list > would work, but there is no alternative to readline() with which I > could read the input in batch mode? I'm asking this because I'd like > to have the program work in both interactive and batch mode. > > Thanks again, > Gang > > > On Wed, Feb 8, 2012 at 12:50 AM, ilai <keren at math.montana.edu> wrote: >> Ahh, >> I think I'm getting it now. Well, readlines() is not going to work for >> you. The help file ?readline clearly states "In non-interactive use >> the result is as if the response was RETURN and the value is ?""?." >> The implication is you cannot use it to "insert" different answers as >> if you were really there. >> How about using eval() instead? You will need to make the answers a >> named list (or just assigned objects). >> >> test <- expression({ >> ?if(a>2) print('+') >> ?else print('I got more') >> ?b <- b+3 ? # reassign b in the environment >> ?print(b) >> ?print(c) >> ?d^2 >> }) >> dump('test',file='myTest.R') ; rm(test) >> >> # make the answers.R file: >> >> a=5 ; b=2 ; c=2 ; d=3 >> source("myTest.R") >> eval(test) >> >> # Now, from the terminal ?R CMD BATCH answers.R out.R >> # And here is my $ cat out.R >> ... flushed >>> a=5 ; b=2 ; c=2 ; d=3 >>> source("myTest.R") >>> eval(test) >> [1] "+" >> [1] 5 >> [1] 2 >> [1] 9 >>> >>> proc.time() >> ? user ?system elapsed >> ?0.640 ? 0.048 ? 0.720 >> >> Would this work? >> Elai >> >> >> >> >> On Tue, Feb 7, 2012 at 4:05 PM, Gang Chen <gangchen6 at gmail.com> wrote: >>> Suppose I create an R program called myTest.R with only one line like >>> the following: >>> >>> type <- as.integer(readline("input type (1: type1; 2: type2)? ")) >>> >>> Then I'd like to run myTest.R in batch mode by constructing an input >>> file called answers.R with the following: >>> >>> source("myTest.R") >>> 1 >>> >>> When I ran the following at the terminal: >>> >>> R CMD BATCH answer.R output.Rout >>> >>> it failed to pick up the answer '1' from the 2nd line in answers.R as >>> shown inside output.Rout: >>> >>>> source("myTest.R") >>> input type (0: quit; 1: type1; 2: type2)? >>>> 1 >>> [1] 1 >>> >>> What am I missing here? >>> >>> Thanks in advance, >>> Gang
xlab = needs to be names = see ?boxplot boxplot(jdata[,"x"],jdata[,"y"],jdata[,"z"],names=c("x values","yvalues","zvalues")) -----Original Message----- From: John Sorkin Sent: Wednesday, February 08, 2012 6:48 PM Cc: r-help Subject: [R] x-axis label for boxplot I am trying to produce three boxplots and label the x axis values: xvalues, yvalues, zvalues. My code is below. The labels are not below the three boxplots. If you could show me how to get the labels below the columns I would be appreciative. John jdata<-data.frame(x = rnorm(100),y=rnorm(100,1),z=rnorm(100,-1),y=runif(100)) boxplot(jdata[,"x"],jdata[,"y"],jdata[,"z"],xlab=c("x values","yvalues","zvalues")) John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:8}}
On Feb 8, 2012, at 7:48 PM, John Sorkin wrote:> I am trying to produce three boxplots and label the x axis values: > xvalues, yvalues, zvalues. > > My code is below. The labels are not below the three boxplots. If > you could show me how to get the > labels below the columns I would be appreciative. > John > > jdata<-data.frame(x = > rnorm(100),y=rnorm(100,1),z=rnorm(100,-1),y=runif(100)) > > boxplot(jdata[,"x"],jdata[,"y"],jdata[,"z"],xlab=c("x > values","yvalues","zvalues")) >You seem to be suffering under the delusion (fostered by the naming conventions and the state of documentation that does a poor job of distinguishing these issues) that "xlab" and "ylab" are going to be labels for individual groups or values along axes. They are not. Instead (.. and using something more generic for xlab like "Grouping") axis( 1, at=1:3, labels=c("x values","yvalues","zvalues") ) -- David.> John David Sorkin M.D., Ph.D. > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > Confidentiality Statement: > This email message, including any attachments, is for th...{{dropped: > 6}} > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
Thank you! John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)>>> "Robert Baer" <rbaer at atsu.edu> 2/8/2012 8:07 PM >>>xlab = needs to be names = see ?boxplot boxplot(jdata[,"x"],jdata[,"y"],jdata[,"z"],names=c("x values","yvalues","zvalues")) -----Original Message----- From: John Sorkin Sent: Wednesday, February 08, 2012 6:48 PM Cc: r-help Subject: [R] x-axis label for boxplot I am trying to produce three boxplots and label the x axis values: xvalues, yvalues, zvalues. My code is below. The labels are not below the three boxplots. If you could show me how to get the labels below the columns I would be appreciative. John jdata<-data.frame(x = rnorm(100),y=rnorm(100,1),z=rnorm(100,-1),y=runif(100)) boxplot(jdata[,"x"],jdata[,"y"],jdata[,"z"],xlab=c("x values","yvalues","zvalues")) John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:18}}