Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caret doparallel in Rmarkdown: Missing verbose info when using render()

I have the following simple example Rmarkdown document (test.Rmd):

---
title: "Test Knit Caret Paralell VerboseIter"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

require(caret)
require(doParallel)


```

## data

```{r data}

set.seed(998)
training <- twoClassSim()

```

## model

```{r fitmodel}
fitControl <- trainControl(
  method = "repeatedcv",
  number = 3,
  repeats = 2,
  verboseIter = T)


ncores <- detectCores()-1

cl <<- makePSOCKcluster(ncores, verbose = TRUE, outfile = "")
registerDoParallel(cl)

set.seed(825)
Fit <- train(Class ~ ., 
             data = training, 
             method = "nnet", 
             trControl = fitControl,
             trace = FALSE
)
stopCluster(cl)
registerDoSEQ()
```

## results

```{r results}
Fit
```

I have several options to run this code or knit the document

  1. Use 'Run all chuncks' in Rstudio
  2. Use Knit button in Rstudio
  3. Knit document with render("test.Rmd")

The following happens

  1. No info gets printed in output or console on iterations
  2. Info gets printed in the R markdown panel
  3. No info gets printed in console

In the project I work on I want to knit the document with different parameters, so I want to use the last option. However I also want to see the progress on fitting the model. Therefor I want to use option 3.

How can I get the info of the iterations printed in console when the documents are rendered?

This is the expected output I want to see:

+ Fold1.Rep1: size=1, decay=0e+00 
+ Fold1.Rep1: size=3, decay=0e+00 
+ Fold1.Rep1: size=5, decay=0e+00 
- Fold1.Rep1: size=1, decay=0e+00 
+ Fold1.Rep1: size=1, decay=1e-01 
- Fold1.Rep1: size=3, decay=0e+00 
+ Fold1.Rep1: size=3, decay=1e-01 
- Fold1.Rep1: size=5, decay=0e+00 
+ Fold1.Rep1: size=5, decay=1e-01 
- Fold1.Rep1: size=1, decay=1e-01 
+ Fold1.Rep1: size=1, decay=1e-04 
- Fold1.Rep1: size=3, decay=1e-01 
+ Fold1.Rep1: size=3, decay=1e-04 
- Fold1.Rep1: size=1, decay=1e-04 
etc.
like image 341
Wietze314 Avatar asked Oct 31 '19 09:10

Wietze314


1 Answers

This may produce what you're looking for, adapted from here, it essentially replicates when you use the knit button in rstudio, which produces the verbose from train, however using this method you should be able to pass in parameters to render. Just change the path to the wd of your rmd file

owd = setwd("path/to/your-Rmd-directory")
system2("Rscript", c("-e", shQuote("library(rmarkdown); render('test.Rmd')"),
            system2("html", "test.html"),
            setwd(owd)))
like image 86
Matt Avatar answered Oct 19 '22 07:10

Matt