Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use sink and still get messages printed in R?

Tags:

r

printing

sink

I am trying to save the log of a boosting using sink function, as following code:

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

However I can't see "what's happening" since it's not printing the function's output and/or message.

My question is how can I can retrieve both a .txt file with sink and see what the function (in this case would be xgb.cv) is printing?

Thank you!

like image 795
patL Avatar asked Mar 07 '23 08:03

patL


1 Answers

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

As you can see above it will both print on the console and you will also find a test.txt file in your current directory.

like image 102
LyzandeR Avatar answered Mar 11 '23 09:03

LyzandeR