---
title: "Using nettskjemar"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using nettskjemar}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  markdown: 
    wrap: sentence
---

```{r}
#| label: setup
#| include: false

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = TRUE,
  error = FALSE,
  fig.path = "static"
)

vcr::setup_knitr(prefix = "ns-")
nettskjemar:::mock_if_no_auth()
```

Nettskjemar connects to version 3 of the [nettskjema api](https://api.nettskjema.no/v3/swagger-ui/index.html), and the main functionality here is to download data from a form into R.
Once you have [created a nettskjema client](authentication.html), and set up your Renvironment locally, you can start accessing your forms.

## General recommendations

While functions to download data also have the option to turn off the codebook, i.e. return the data with the original questions as column names, this is **not recommended**.
Working with data in R in this format is very unpredictable, and we cannot guarantee that the functions in this package will act as expected.

Therefore, you are **highly advised** if you are using this package, to turn on the codebook in the Nettskjema portal for your form, and setting up a codebook for the entire form.

You can toggle the codebook for a form by going to the Nettskjema portal and entering your form.
Then proceed to "Settings" and then "General settings", and make sure "Codebook activated" is set to "Yes".
Once this is toggled, you will need to setup the codebook, either manually (advised) or by using the pre-filling functionality in Nettskjema.

You can read more about the details around the codebook on the [UiO webpages](https://www.uio.no/tjenester/it/adm-app/nettskjema/hjelp/kodebok.html) (only available in Norwegian).

## [Tidyverse](https://www.tidyverse.org/) compatible

The data returns in this package are developed to be tidyverse-compatible.
This means that those who are familiar with tidyverse, should find working with the data as retrieved from this package fairly easy.
If you want to learn about the tidyverse and how to use it, there are excellent resources for that on the [Tidyverse webpage](https://www.tidyverse.org/learn/).

## Download submissions

Perhaps at the core of this package is the ability to download submission answers to a form into a tibble (variation of a data.frame).

```{r}
#| label: data
#| cassette: true

library(nettskjemar)
formid <- 123823

data <- ns_get_data(formid)
data
```

You will notice three columns that are prefixed with `$`.
These are columns automatically added by the Nettskjema backend to your data, and the prefix is used to denote exactly this.

If you'd like to have labelled data, similar to that of SPSS and Stata, have a look at the [vignette on labelled data](labelled_data.html) for more information about that.

### Fetching Raw Data

Raw answers come in a very different format than the data as seen in the codebook and preview in Nettskjema portal.
The raw data have a timestamped format, per question for each submission.
This means the data comes in a _tall_ format (many rows per submission) rather than a wide format (one row per submission).
The raw data may provide those who have keen interest in the time a users spent between each question.
The data.frame this output will have one column that indicates which question the row is for, and another which is the response to that question.

```{r}
#| label: data-raw
#| cassette: true

# Fetch raw data
ns_get_data(formid, type = "long")
```

Raw data cannot be labelled, and no other alterations have been made to the data.
They come exactly as the API returns them, so you may do what you need with them.


### Controlling checkbox output

The Nettskjema survey tool includes the possibility to create a matrix of checkboxes, i.e. giving the respondents the ability to select several options within a question.
Checkboxes are returned as binary columns, one columns per checkbox where 1 indicates that the checkbox was ticked.

We provide some extra functionality to help work with these data. 
Currently we only support working with the checkbox matrix questions, but are working on a solution for all checkboxes.

```{r}
#| label: multi-list
#| cassette: true

ns_get_data(formid) |>
  ns_alter_checkbox(
    to = "list"
  )
```

These can be separated into rows if wanted, using tidyverse syntax.

```{r}
#| label: multi-label
#| cassette: true

library(tidyr)
library(dplyr)

# As list column
ns_get_data(formid) |>
  ns_alter_checkbox(
    to = "list"
  ) |>
  relocate(checkbox_matrix.1, .after = 2)


# Turns list column, into rows
ns_get_data(formid) |>
  ns_alter_checkbox(
    to = "list"
  ) |>
  unnest(checkbox_matrix.1) |>
  relocate(checkbox_matrix.1, .after = 2)
```

If you are not used to working with list columns,
another option is to work with delimited strings in columns.
In this procedure, you can turn the checkboxes into a character column, where each option in separated by a character of your choosing.

```{r}
#| label: multi-delim
#| cassette: true

# As delimited string column
ns_get_data(formid) |>
  ns_alter_checkbox(
    to = "character",
    sep = ";"
  ) |>
  relocate(checkbox_matrix.1, .after = 2)

# Turns string column, into rows
ns_get_data(formid) |>
  ns_alter_checkbox(
    to = "character",
    sep = ";"
  ) |>
  relocate(checkbox_matrix.1, .after = 2) |>
  separate_rows(checkbox_matrix.1)
``` 


