在 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)
```
如何避免在我的 html 文件上进行 train
迭代打印?
最佳答案
按照 @Swiss12000's comment 中的建议,可以将参数 trace = FALSE
传递给 train
以抑制消息。
此行为未记录在 ?train
中,因为参数被传递(通过 ...
)到方法 nnet
。 trace = 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 消息 = 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
关于删除 knit html 输出上的 caret::train() 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35446743/