Skip to content

Setting up infile for R CMD BATCH

8 messages · ilai, Robert Baer, David Winsemius +2 more

#
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:
#
Sorry Elai for the confusions.

Let me try to reframe my predicament. The main program "myTest.R" has
been written in interactive mode with many readline() lines embedded.
Suppose a user has already run the program once before in interactive
mode with all the answers saved in a text file called answer.R. Now
s/he does not want to go through the interactive again because it's a
tedious process, and would like to run the program but in batch mode
with answer.R. And that's why I tried the following which didn't pan
out:

R CMD BATCH answer.R output.Rout

Of couse I could rewrite a separate program specifically for batch
mode as you suggested previously with eval(), for example. However, is
there an approach to keeping the original program so that the user
could run both interactive and batch mode? That probably requires
modifying the readline() part, but how?

Thanks,
Gang
On Wed, Feb 8, 2012 at 2:08 PM, ilai <keren at math.montana.edu> wrote:
#
I'm going to declare this SOLVED. Yes, if you don't want a separate
script for batch, you will need to modify the original script so it
either readline or skips it. Here is an example:

# Save in file myTest.R
####################
# Add this local function to the beginning of your original "program"
and replace all the readline prompts with myreadline.
# The new function takes on two arguments:
# "what": the original object (in your example it was "type"<-...)
# "prompt": The same string from readline
# All it is doing is searching for "Answers.R", sourcing if available
or prompting if not.

myreadline <- function(what,prompt){
  ans <- length(grep('Answers.R',list.files(),fixed=T))>0  # add a
warning for multiple files
  if(ans) source('Answers.R')
  else{
    ret <- as.integer(readline(prompt))
    assign(what,ret,1)
  }
}

# here is an interactive program to square only negative values

print(x <- rnorm(1))
myreadline('type','x>0 ? 1:T,2:F \n')
print(x^type)

### END myTest.R ####

Running myTest interactivly:
[1] -0.3712215
x>0 ? 1:T,2:F
2
[1] 0.1378054           # -.37^2
[1] 0.3160747
x>0 ? 1:T,2:F
1
[1] 0.3160747          # .316^1

# Create a list of answers
# run again this time with answers available
[1] -1.088665   # skips prompt
[1] -1.088665   # -1.088^1 (type in Answer.R ==1)

# Now you can also run as batch
$ R CMD BATCH myTest.R out.R
$ cat out.R
# ...
[1] -1.248487
[1] -1.248487

That's it. Only thing is to keep the file names in the working
directory straight.

Enjoy
On Wed, Feb 8, 2012 at 12:39 PM, Gang Chen <gangchen6 at gmail.com> 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"))

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}}
#
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:

            
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") )
#
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)
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}}
#
Hi Elai, yes, the approach works out pretty well. Thanks a lot for
spending time on this and for the great help!

Gang
On Wed, Feb 8, 2012 at 5:43 PM, ilai <keren at math.montana.edu> wrote: