如何在指令碼中包含互動式輸入以從命令列執行
我正在嘗試寫一個互動的R指令碼.例如:
try.R:
print("Entr some numbers. >",quote=F) a = scan(what=double(0)) print a q()
現在,如果我在命令列上執行它
$R --no-save < try.R
它嘗試從try.R獲取stdin,給出以下錯誤:
> print("Entr some numbers. >",quote=F) [1] Entr some numbers. > > a = scan(what=double(0)) 1: print a Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,: scan() expected 'a real', got 'print' Execution halted
我嘗試了一些其他的方法,但都給出錯誤.例如:
$R CMD BATCH try.R $Rscript try.R
那麼如何編寫一個從* nix shell命令列工作的R指令碼,並可以從使用者那裡獲取互動式輸入?
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/3935288/how-to-include-interactive-input-in-script-to-be-run-from-the-command-line