[英]Print the output of a function in terminal
我在 R 中定義了以下 function。
#!/usr/bin/Rscript
fahr_to_kelvin <- function(temp) {
kelvin <- ((temp - 32) * (5 / 9)) + 273.15
print("Hello World")
return(kelvin)
}
當我在 R 控制台中輸入fahr_to_kelvin(32)
時,它返回 273.15(沒有打印“Hello World”,我不知道為什么:)無論如何,我嘗試在下面編寫代碼:
chmod +x ~/tuning1/Fun1.R
~/tuning1/Fun1.R
但是,終端中沒有出現任何內容(沒有錯誤)。 你能幫我為什么它不工作嗎?
問題是腳本文件只包含function的聲明,沒有實際的調用。
在腳本末尾添加新行以調用 function,或者更好地從命令行讀取參數:
#!/usr/bin/Rscript
fahr_to_kelvin <- function(temp) {
kelvin <- ((temp - 32) * (5 / 9)) + 273.15
print("Hello World")
return(kelvin)
}
args <- commandArgs(trailingOnly=TRUE)
if (length(args) > 0) {
fahr_to_kelvin(as.numeric(args[1]))
} else {
print("Supply one argument as the numeric value: usage ./Fun1 temp")
}
$ Rscript Fun1.R 32
[1] "Hello World"
[1] 273.15
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.