---
title: "Importing a (pre)registration form from embedded JSON from a URL"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Importing a (pre)registration form from embedded JSON from a URL}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

(Pre)registration forms can be directly imported from an URL. For example, the vignette about how to create a preregistration form stores the two forms embedded as JSON. These can be imported again as a `preregr` form.

```{r}
importedExample <-
  preregr::import_from_html(
    "https://preregr.opens.science/articles/creating_prereg_form.html"
  );
```

This imports the first form, the example that is created in that vignette:

```{r}
importedExample;
```

Or, to view the form:

```{r}
preregr::form_show(importedExample);
```

## Initiating a (pre)registration from an imported form

We can now initialize a new (pre)registration using this form:

```{r}
freshPrereg <-
  preregr::prereg_initialize(
    importedExample
  );
```

Et voila, an empty (pre)registration:

```{r}
freshPrereg;
```

## Selecting the form to import

To import the second form that is embedded in that vignette (the full form for systematic reviews), we can pass a value for `select`:

```{r}
importedExample_2 <-
  preregr::import_from_html(
    "https://preregr.opens.science/articles/creating_prereg_form.html",
    select = 2
  );
```

If we now knit this form into this vignette again, it will again be included as JSON as well:

```{r}
preregr::form_knit(importedExample);
```

## Saving a form to a spreadsheet

Alternatively, you can export the form to a local spreadsheet file so you can import it later:

```{r, eval=FALSE}
preregr::form_to_xlsx(
  formExample,
  file = "C:/Data/Research/amazing-new-project/prereg-form.xlsx"
);
```
