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

2

7

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.
Cacique answered 31/10, 2019 at 9:56 Comment(0)
N
2

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)))
Nedi answered 13/11, 2019 at 1:55 Comment(3)
It works. But is there any way to stop de proces? When I stop in Rstudio the process seems to continue in the background.Cacique
@Cacique Using the stop in Rstudio or the Esc key works for me. You could also set a timeoutNedi
I used stop in Rstudio, and it looked like it stopped. But when I look at task manager, all cores are still running on 100%. Only when I manually shut down all R sessions in task manager it really stops.Cacique
A
0

You can add a knit_hook at the top of the Rmd file to capture the console outputs and print them while rendering.

```{r, include=FALSE}
print_output <- function(x, options) {
  cat(x)
  return(x)
}
knitr::knit_hooks$set(chunk = print_output)
```
Adina answered 9/11, 2019 at 1:45 Comment(3)
Thank you for the reply. I tried your solution, but it kind of works, but the output intended for the markdown file, gets printed to the console only. How can I make sure, the output intended for the markdown is printed in the output file (too).Cacique
@Cacique Edited. If the function returns the input, it will also be printed in the knitted file. output needs to be changed to chunk to make the fonts consistent. See yihui.name/knitr/hooks.Adina
I also noticed that the output is printed only when the chunk code has been fully executed. So the progress during learning is not printed, which I wanted to track.Cacique

© 2022 - 2024 — McMap. All rights reserved.