我怎样才能使用接收器并仍然在 R 中打印消息?

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()

但是我看不到 "what's happening",因为它没有打印函数的输出 and/or 消息。

我的问题是如何才能用 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 文件。