---
title: "Visualizing progress"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Visualizing progress}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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


When a specification curve analysis requires the estimation of several thousand models, the procedure can take quite a while. In those cases, it would be nice to have some sort of indicator of the progress (both to feel confident that the procedure is still running and to have an idea of how long the procedure will still take).

## 1. Create customized model function

To run the specification curve with a progress bar, we need to include `pb$tick()` in the model function. 

```{r}
library(specr)

# Set up specific lm function
linear <- function(formula, data) {
  pb$tick()
  glm(formula = formula, data = data, family = gaussian(link = "identity"))
}
```

## 2. Provide number of ticks

Next, we can use the function `progress_bar()` from the package [progress](https://github.com/r-lib/progress). The only thing we need to due is provide the number of ticks (i.e., operations) that will be computed. Attention: If you want to rerun the analysis, you also need to reinitialize this `pb` object.

```{r, message = F, warnings = F}
library(progress)

# Provide number of ticks
pb <- progress_bar$new(format = "[:bar] | :percent (:current/:total) | Finished in:eta",
                       width = 100,
                       total = 192)
```

## 3. Run analysis

```{r}
# run spec analysis
results <- run_specs(example_data,
                     y = c("y1", "y2"),
                     x = c("x1", "x2"),
                     model = "linear",      # use customized function
                     controls = c("c1", "c2"),
                     subset = list(group1 = unique(example_data$group1),
                                   group2 = unique(example_data$group2)))

```

The console will show a progress bar during estimation. For more information, see the [documentation](https://github.com/r-lib/progress) of the package `progress`. 

```{r, eval=F}
[==========================================================-------------]  81% (156/192) Finished in 1s
```


