![](/img/trans.png)
[英]In R, how to know if a function printed warning messages to the console before return?
[英]How can I use sink and still get messages printed in R?
我試圖使用接收sink
功能保存增強的日志,如下代碼:
require(xgboost)
require(R.utils)
data(iris)
train.model <- model.matrix(Sepal.Length~., iris)
dtrain <- xgb.DMatrix(data=train.model, label=iris$Sepal.Length)
xgb_grid = list(eta = 0.05, max_depth = 5, subsample = 0.7, gamma = 0.3,
min_child_weight = 1)
sink("evaluationLog.txt")
fit_boost <-xgb.cv(data = dtrain,
nrounds = 1000,
objective = "reg:linear",
eval_metric = "logloss",
params = xgb_grid,
colsample_bytree = 0.7,
early_stopping_rounds = 100,
nfold = 5,
prediction = TRUE,
maximize = FALSE
)
sink()
但是我看不到“發生了什么”,因為它不打印函數的輸出和/或消息。
我的問題是如何才能檢索帶有接收sink
的.txt
文件,看看該函數(在本例中是xgb.cv
)是打印的?
謝謝!
使用參數split
:
sink('test.txt', split = TRUE)
print(letters)
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
#[18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
sink()
如上所示,它將在控制台上打印,您還可以在當前目錄中找到test.txt
文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.