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.
phiver
  • 23,048
  • 14
  • 44
  • 56
Wietze314
  • 5,942
  • 2
  • 21
  • 40

2 Answers2

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)))
Matt
  • 2,947
  • 1
  • 9
  • 21
  • It works. But is there any way to stop de proces? When I stop in Rstudio the process seems to continue in the background. – Wietze314 Nov 15 '19 at 09:30
  • 1
    @Wietze314 Using the `stop` in Rstudio or the `Esc` key works for me. You could also set a `timeout` – Matt Nov 15 '19 at 12:06
  • 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. – Wietze314 Nov 15 '19 at 20:44
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)
```
Feng Mai
  • 2,749
  • 1
  • 28
  • 33
  • 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). – Wietze314 Nov 10 '19 at 19:44
  • @Wietze314 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 https://yihui.name/knitr/hooks/. – Feng Mai Nov 10 '19 at 22:23
  • 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. – Wietze314 Nov 11 '19 at 21:14