在 knit html 输出上删除 carett::train() 的迭代

Remove iterations of carett::train() on knit html output

在 Rstudio

中将以下代码与 Knit HTML 结合使用
---
title: "test"
output: html_document
---

```{r pressure, echo=FALSE}
library(caret)
tc <- trainControl(method="boot",number=25)
train = train(Species~.,data=iris,method="nnet",trControl=tc)
confusionMatrix(train)
```

如何避免 train 迭代打印在我的 html 文件上?

中所建议,可以将参数 trace = FALSE 传递给 train 以抑制消息。

此行为未记录在 ?train 中,因为参数被传递(通过 ...)到方法 nnettrace = FALSE 仅适用于支持此参数的方法。在其他情况下,下面的 capture.output 方法可能仍然有用。


caret::train 主动将消息打印到 stdout。那太讨厌了。可以通过将表达式包装在 capture.output():

中来抑制输出
garbage <- capture.output(train <- train(Species~.,data=iris,method="nnet",trControl=tc))

请注意,这是 differentce between the assignment operators 重要的情况之一:capture.output(train = train(... 不起作用,可能是因为赋值被解释为 train() 的参数。

为了额外抑制包启动消息,添加 chunk option message = FALSE

---
title: "test"
output: html_document
---

```{r echo=FALSE, message = FALSE}
library(caret)
tc <- trainControl(method = "boot",number = 25)
garbage <- capture.output(
  train <- train(Species ~ ., data = iris, method = "nnet", trControl = tc))
confusionMatrix(train)
```

输出:

test

## Bootstrapped (25 reps) Confusion Matrix 
## 
## (entries are percentages of table totals)
##  
##             Reference
## Prediction   setosa versicolor virginica
##   setosa       33.8        0.0       0.0
##   versicolor    0.0       31.0       1.1
##   virginica     0.0        2.0      32.1