---
title: "gmoTree Codebooks"
author: "Patricia F. Zauchner"
date: "2023-09-21 (updated: 2025-02-04)"
output:
  rmarkdown::html_vignette:
    toc: yes
vignette: >
  %\VignetteIndexEntry{gmoTree Codebooks}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r collapse=TRUE, include=FALSE}
library(gmoTree)
```

# Overview

With the `codebook()` function, you can generate codebooks based on your <a href="https://www.otree.org/" target="_blank">oTree</a> (Chen et al., 2016) code. This vignette introduces its key features and functionalities.

Disclaimer: This code was tested with both custom implementations and official oTree sample codes.^[The oTree sample codes can be accessed when <a href="https://otree.readthedocs.io/en/latest/install-nostudio.html#install-nostudio" target="_blank">setting up an oTree project</a> (Chen et al., 2016). All examples presented in this vignette are based on the official oTree sample code, with minor modifications to meet the needs of this package.] However, due to the complexity of oTree setups, it may not address all variations. Please review your output carefully, and feel free to open a <a href="https://github.com/ZauchnerP/gmoTree/issues" target = "_blank">GitHub issue</a> if you encounter any problems.

`Session` and `Participant` variables as well as built-in variables are not shown in the codebooks! See also further important limitations of this function in the details section of `codebook()`.

# Running the code

In its simplest form, the function requires only the path to your oTree code folder. The following code reads the contents of your oTree folder, saves a PDF file containing your codebook in your working directory, and returns a list named `cbook`, containing the extracted information.

```
cbook <- codebook("D:/pathtoyourproject/oTree_codes")
```

# Codebook structure and output

## File output

The output file is structured as follows: Each application is represented as a chapter (heading level 1), with subchapters for the variables from the `Group` and `Player` classes (heading level 2). The display of the `Constants` class is optional and enabled by default. It can be disabled with the argument `include_cons = FALSE`. The display of the `Subsession` class is also optional, but disabled by default. I can be enabled with `include_subs = TRUE`.

<img src= "structure.png" style = "border:1px solid" alt="Image showing an example of a heading structure of a PDF file with Level 1 and Level 2 headings">

The example below shows the output for the `cournot` app, which includes all variables from the `Constants`, `Group`, and `Player` classes. If a class contains no custom variables, the class heading will still appear, accompanied by the message “No model fields except the built-in ones.”

<img src="app.png" style = "border:1px solid" alt="Image showing the cournot app information in the PDF output">

## List output

Use `output = "list"` to bypass file creation. This option returns all the information in list format, which can be used in R Markdown for custom codebooks. Below is an example of such a list output. (The argument `doc_info = FALSE` suppresses messages about missing documentation for variables.)


```{r cournot codebook}
# Create the codebook list
cbook <- codebook(path = system.file(path = "extdata/ocode_new",
                                     package = "gmoTree"),
                  output = "list",
                  doc_info = FALSE)

str(cbook, 1)  # Show all apps
print(cbook$cournot)  # Only show cournot app
```

# Choosing what to show

By default, all apps are included in the codebook. To generate a codebook for only one app, use the `app` argument. For example, to generate a codebook for the `cournot` app:

```{r one app}
cbook <- codebook(
  path = system.file("extdata/ocode_new",
                     package = "gmoTree"),
  fsource = "init",
  output = "list",
  doc_info = FALSE,
  app = "cournot")

str(cbook)
```

The code above returns a list with the app-specific information. You can also generate codebooks for multiple apps by specifying them in the `app` argument.

```{r two apps}
cbook <- codebook(
  path = system.file("extdata/ocode_new",
                     package = "gmoTree"),
  fsource = "init",
  output = "list",
  doc_info = FALSE,
  app = c("cournot", "dictator"))

str(cbook, 1)
```

You can also show all apps except for one or more. Here is an example that removes the app `prisoner`:

```{r removing apps}
cbook <- codebook(
  path = system.file("extdata/ocode_new", package = "gmoTree"),
  fsource = "init",
  output = "list",
  doc_info = FALSE,
  app_rm = "prisoner")

str(cbook, 1)
```

# Customizing how the codebook is shown

## Customizing the codebook titles and metadata

You can easily modify the titles, subtitles, dates, and author names in your codebooks using the following arguments:

```
codebook(
    path = "Your_oTree_path",  # Can be either absolute or relative!
    title = "Codebook",
    subtitle = "Example Project",
    date = "today",
    params = list(author = c("Max Mustermann", "John Doe"))
)
```

<img src="title.png" style = "border:1px solid" alt="Image showing the title section in a PDF output">


## Sorting the code

You can sort the apps in the codebook by specifying the `sort` argument. For example:

```{r sort apps}
cbook <- codebook(
  path = system.file("extdata/ocode_new", package = "gmoTree"),
  fsource = "init",
  output = "list",
  doc_info = FALSE,
  app = c("cournot", "dictator"),
  sort = c("dictator", "cournot"))

str(cbook, 1)
```

## Referring to settings

The `gmoTree` package can automatically replace references to variables in `settings.py` with the values in the `settings.py` file. To enable this, set `settings_replace = "global"`.

```{r global settings}
cbook <- codebook(
  path = system.file("extdata/ocode_new", package = "gmoTree"),
  fsource = "init",
  output = "list",
  settings_replace = "global",
  app = "dictator")

print(cbook$dictator$Constants$Variable)
```


You can also choose not to replace the references to settings variables by using `settings_replace = NULL`.

```{r do not replace settings}
cbook <- codebook(
  path = system.file("extdata/ocode_new", package = "gmoTree"),
  fsource = "init",
  output = "list",
  settings_replace = NULL,
  app = "dictator")

print(cbook$dictator$Constants$Variable)
```

It is also possible to provide a list of variables to replace the settings variables with the argument `user_settings`. For example:

```{r user settings}
cbook <- codebook(
  path = system.file("extdata/ocode_new", package = "gmoTree"),
  fsource = "init",
  output = "list",
  settings_replace = "user",
  user_settings = list(variable = "someValue"),
  app = "dictator")

print(cbook$dictator$Constants$Variable)
```

# Saving your codebook

When you choose `output = "file"` or `output = "both"`, your codebook will be saved as a professional document.

## Output formats

The argument `output_format` allows you to choose the format of the exported codebook file.
Available formats are:

* `html_document`
* `word_document`
* `latex_document`
* `odt_document`
* `rtf_document`
* `md_document`
* `pdf_document`
* `pdf_document_simple`

You can choose the format that best suits your needs, with all formats except PDF allowing easy post-generation editing.

When knitting to PDF, make sure to have LaTex installed! It is recommended to use `output_format = pdf_document_simple`. However, some non-Latin characters (e.g., Chinese characters) may not work with this format. In such cases, you can choose `output_format = pdf_document`, which is set to use `xelatex`.

Please note that using `xelatex` with gmoTree has some limitations: for example, `gmoTree` currently cannot handle long variable values that result in excessively long table cells. This can cause the PDF to display improperly in certain viewers, such as Nitro. To address this, carefully review your file. If necessary, consider using `output_format = latex_document` to generate a `.tex` file, which you can then compile into a PDF. Alternatively, open the file with a PDF viewer that can handle these cases, then save it there to ensure it displays correctly in other PDF viewers.

## Output files

By default, `gmoTree` codebooks are saved in your working directory with
the default file name `codebook`, followed by the file extension.
You can modify this by specifying either a custom file name, another directory path, or both.

For example, to save the codebook with a different name, use the following code:

```
codebook(
    path = "C:/Users/username/folder/oTree",  # Replace!
    output_file = "gmoTree_codebook")
```

You can also include the file extension, although it is optional:

```
codebook(
    path = "C:/Users/username/folder/oTree",  # Replace!
    output_file = "gmoTree_codebook.pdf")
```

You can save the file in a subfolder within the current directory by including the absolute or relative folder name in the `output_file` argument. For example:

```
codebook(
    path = "C:/Users/username/folder/oTree",  # Replace!
    output_file = "codebookfiles/gmoTree_codebook.pdf")
```

You can also specify absolute paths, either in the `output_dir` argument or directly within the file name specified in the `output_file`.
For example:

```
codebook(
    path = "C:/Users/username/folder/oTree",  # Replace!
    output_dir  = "C:/Users/username/folder/codebooks",
    output_file = "mycodebook")
```

# Summary

This vignette documents how to customize the codebook generated by the `codebook()` function, including options for content selection, formatting, and exporting to various formats. These tools enable the creation of comprehensive and tailored documentation for your experimental data.

# References

Chen, D. L., Schonger, M., & Wickens, C. (2016). oTree—An open-source platform for laboratory, online, and field experiments. *Journal of Behavioral and Experimental Finance*, *9*, 88–97. https://doi.org/10.1016/j.jbef.2015.12.001