Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

knitr

r-caret

Using the following code with Knit HTML in Rstudio

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

How to avoid the train iteration print on my html file?

like image 771
S12000 Avatar asked Feb 17 '16 02:02

S12000


1 Answers

As suggested in @Swiss12000's comment, the argument trace = FALSE can be passed to train to suppress the messages.

This behaviour is not documented in ?train because the parameter is passed (via ...) to the method nnet. trace = FALSE will only work for methods that support this parameter. In other cases, the capture.output approach below might still be useful.


caret::train prints messages to stdout, unsolicitedly. That's nasty. The output can be suppressed by wrapping the expression in capture.output():

garbage <- capture.output(train <- train(Species~.,data=iris,method="nnet",trControl=tc))

Note that this is one of the cases where the differentce between the assignment operators matters: capture.output(train = train(... doesn't work, probably because the assignment is interpreted as an argument to train().

In order to additionally suppress the package startup messages, add the 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)
```

Output:

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
like image 126
CL. Avatar answered Oct 14 '22 01:10

CL.