2013年1月9日 星期三

[R] 交互對談指令

其實是一個小技巧, 因為某人是程式白癡, 我又不可能幫他跑所有可能組合...
所以只能把程式丟給他跑, 為了要讓介面人性化一點, 決定要用"交談式"的參數輸入~~

舉例來說, 下面的小函數就可以由使用者決定一個方陣的大小...
只要把這個code給copy/paste到R去, 就可以用demo()來跑程式了...


demo <- font="font" function="function">
    cat(" - Please input the size of matrix: ")
    n = readLines(n=1)
    n = as.numeric(n)
    mat = matrix(1,ncol=n,nrow=n)
    print(mat)
}

> demo()
 - Please input the size of matrix: 3
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1




沒有留言: