expr + newlineWhereas this appears to the R interpreter as two seperate expressions on two different lines
expr
expr newline
+ expr
In file etest.R:The same example also works when the entire function is typed directly to the command line interpreter.etest <- function( k ) { if ( k > 0 ) { cat("k is greater than 0\n" ) } else { cat("k is NOT greater than 0\n" ) } }Execution:> source("etest.R") > etest(0) k is NOT greater than 0 > etest(1) k is greater than 0
contents of file etest2.R:This is because outside of function definitions, the parser is looking for the first complete command it finds at the end of each newline. This can be prevented by putting the else clause on the same line as the } that ends the then clause:if ( k > 0 ) { cat("k is greater than 0\n" ) } else { cat("k is NOT greater than 0\n" ) }when this is sourced,> source("etest2.R") Error in parse(file, n = -1, NULL, "?") : syntax error at 3: { cat("k is greater than 0\n" ) } 4: else
see Discussion at list R-helpk <- 0 if ( k > 0 ) { cat("k is greater than 0\n" ) } else { cat("k is NOT greater than 0\n" ) }when this is sourced, it works properly> source("etest3.R") k is NOT greater than 0
hist( q, -1:max(q) )
to make a list of lists with initial empty members:bugs <- list( list(dels=c(),silents=c()) ) for ( i in 2:n_bugs ) bugs <- c( bugs, list( list(dels=c(),silents=c()) ) )
gens <- rep( list( list( dels=c(), sils=c() ) ), n_gens )