Try adding --vanilla when calling R, e.g. R --vanilla --no-save --args 5 < test1.R
because I think you're picking up and previously stored session. You'll most likely will find that "res <- conditional1(x)" will give an error saying 'conditional1' is not defined; you need to define the function before using it. My $.02 /H On Mon, Feb 13, 2012 at 1:59 PM, Schmidt, Michael <mschm...@med.miami.edu> wrote: > Hi, > I am new to R. I was trying to get a very simple program to run. Take one > number from the command line. If the number < 0 return -1. If number > 0 > return 1 and if the number == 0 return 0. The code is in a file called test1.R > > > The code: > > #useage: R --no-save --args 5 < test1.R > args = (commandArgs(TRUE)) > x = as.numeric(args[1]) > print(x) > > res <- conditional1(x) > cat("result= ",res,"\n") > > conditional1 <- function(x){ > result <- 0 > if (x > 0) { > result <- 1 > } else if (x < 0) { > result <- -1 > } > return(result) > } > > > The output: >>R --no-save --slave --args 1 < test1.R > [1] 1 > result= 1 >>R --no-save --slave --args -1 < test1.R > [1] -1 > result= -1 >>] R --no-save --slave --args 0 < test1.R > [1] 0 > result= -1 > > > The problem: > For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If the > 0 value is passed into the function named "conditional1" I would expect both > if-statements to evaluate to false and 0 being return. From what I can tell > (0 < 0) evaluates to true since -1 is returned. Hmmmmm... > What is going on? What am I doing wrong? Why is this happening? I am baffled! > Any help would be appreciated. > Thanks > Mike > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.