cc <- function( num=TRUE, #whether to transform the data to numeric quiet=FALSE #whether to print the constructed data in the console ){ cdata <- list() i <- 1 while(TRUE) { cdata[[i]] <- readline(paste(i, " : ", sep="")) if(cdata[[i]]=="..") { i <- i-1 } #work as else if(cdata[[i]]=="") { break } else { i <- i+1 } } i <- i-1 cdata <- unlist(cdata[1:i]) if(i==1){ tmp <- strsplit(cdata, ";")[[1]] tmp <- strsplit(tmp, " ") tmp <- lapply( X=tmp, FUN=function(x) return(x[!(x=="")]) ) if(num) { tmp <- lapply( X=tmp, FUN=as.numeric ) } if( length(tmp)==1 ) { cdata <- unlist(tmp) } else if( !all(sapply( X=tmp, FUN=length )==length(tmp[[1]])) ) { stop("the numbers of column in each row differ.") } else { cdata <- matrix(unlist(tmp), nrow=length(tmp), ncol=length(tmp[[1]]), byrow=TRUE) } i <- length(unlist(tmp)) } else { if(num) { cdata <- as.numeric(cdata) } } if(!quiet) { cat("\ntotal", i, "items were read.\n") if(is.matrix(cdata)) { maxnch <- apply( X=cdata, MARGIN=2, FUN=function(x) max(nchar(x)) ) for(j in 1:nrow(cdata)) { for(k in 1:length(cdata[j,])) { cat( rep(" ", maxnch[k]-nchar(cdata[j,k])), cdata[j,k], " ", sep="") } cat("\n") } } else { cat(cdata, "\n", sep=" ") } cat("\n") } invisible(cdata) } comment(cc) <- c( "cc - Make a vector/matrix using Matlab-like expression or sequential input.", " Just hit cc() and then put in the elements of the vector sequentially.", " You can use two periods <..> to redo the previous element input (works as ).", " Also, you can use a Matlab-like expression (e.g. 1 2 3; 4 5 6; 7 8 9)", "", "SYNOPSIS", " x <- cc()", "INPUT", " num : logical. TRUE when need a numeric vector/matrix. Otherwise, a character vector/matrix is returned.", " quiet : logical. TRUE if you don't need a input data display to the console.", " FALSE recommended in case you forgot to catch the return value by any variable,", " so that you can salvage it from the console by using cc().", "OUTPUT", " x : a vector/matrix.", "", "See http://www7b.biglobe.ne.jp/~homunculus/r/cc.html for detailed instruction in Japanese.", "ver.1.01, written by MOCHI, 2009." )