Preliminaries

You can find the solutions for this exercise as well as the following ones in the exercises folder in the workshop material. You can also navigate the workshop material online (including exercises and solutions).

You can copy code from the exercise and solution files by clicking on the small blue clipboard icon in the upper right corner of the boxes showing the code.

Some of the exercise require you to add text and R code. Feel free to copy from the example paper in the folder exercises/example_manuscript. We do, however, encourage you to use material from a project of your own, if possible. It will be a lot more fun.

Exercises

In this exercise, you will create a first R Markdown document. The example data we use here is taken from the example manuscript provided with the workshop material. However, we encourage you to use your own data for this and the following exercises.

Exercise 1

Create a new R Markdown document with HTML output format and knit it.

Solution

The easiest approach to create a new R Markdown document is to either use the operating system-native menus:

You can also use the RStudio menus:


To knit the document either use the the RStudio button Knit or the corresponding key board shortcut

Ctrl/Cmd + Shift + K.



YAML front matter

Exercise 2

Customize the YAML front matter of your R Markdown document. Add

  1. a meaningful title
  2. an author name
  3. an abstract field to the YAML front matter
  4. a floating table of contents, and
  5. choose a different theme.
Feel free to customize some more! :)
See the documentation for rmarkdown::html_document().

Solution

---
title: "So much meaning"
autor: "Ernst-August Dölle"

abstract: |
  This is my abstract. It's really short!

output:
  html_document:
    theme: "spacelab"
    toc: true
    toc_float: true
---



Markdown

Exercise 3

Add some text to the document. You can copy the example paper introduction or text for a project of your own. Try out some of the text formatting, e.g.,

  • create headings,
  • make some words bold or italic,
  • add an unordered list,
  • add a footnote,
  • add citations, or
  • add an equation.
Try using the visual editor (or citr) for some of your editing.

You can find a quick reference guide to markdown formatting syntax in RStudio. Go to Help > Markdown Quick Reference.

To switch to the visual editor click the button at the top of your Rstudio editor pane.

Don’t forget to add the bibliography file to the YAML front matter for your citations to render properly.

Solution

Add the bibliography files to the YAML front matter:

bibliography: "references.bib"

The following exapmle text is taken from the example article.

One of the most frequently utilized paradigms in the field of implicit learning is the serial reaction time task (SRTT) originating from @nissen_attentional_1987.

...

However, performance under inclusion ($I$) and exclusion instructions was identical (i.e., $I=E$), a finding that is interpreted as indicating the *absence* of explicit knowledge and instead suggests that the sequence knowledge was fully implicit.[^control-sequence-designs]

[^control-sequence-designs]:
Note that the $E>B$ pattern could not be replicated in other studies [@wilkinson_intentional_2004; @norman_fringe_2006] that, as a baseline, compared generation performance with that for a *control sequence* instead of an a-priori fixed value.

...

## The current study

...

We aimed at exploring effects of response tendencies, simple frequency information and unspecific properties of the material, and task properties as well as their interactions.
Focus of the present study is their potential of distorting PD estimates of implicit and explicit learning.
The present study realized three different 'control' conditions without any sequence information: 

- a training phase with randomly drawn permutations of a second-order 8-item sequence, 
- a training phase with randomly drawn response locations (from a uniform distribution), 
- a no-learning condition in which participants merely familiarized themselves with the task.



R code

Exercise 4

Add some analyses code to the document. You can copy the analysis code for the example paper or use code for a project of your own.

  • Create separate chunks for meaningful sets of analytic steps.
  • Add descriptive chunk names.
  • Include a hidden code chunk that creates plot.
Make sure that you use relative paths to access any data files. Ideally place your R Markdown document and the data in a common parent diretory.

Insert a new code chunk either by clicking , or with Ctrl + Alt + I.

Be careful not to break code chunks. The enclosing elements ```{r} and ``` have to be on lines of there own and cannot be preceeded by white space.

See knitr documentation for a complete list of chunk options.

Solution

The following code is taken from the example article. Don’t worry if some of the functions are not familiar. We’ll get to those. :)

```{r laod-data}
acquisition <- readRDS("data/acquisition-task.rds")
```

...

```{r plot-acquisition-rt}
#| echo: false

# Reaction times during SRTT phase (i.e., training or acquisition)
tmp <- acquisition[
  acquisition$error == 0 &
  acquisition$material != "No-learning" & # these Ss didn't work on SRTT
  acquisition$trial > 1 &
  acquisition$included_participant,
]

par(mfrow = c(1, 2)) # place two different plots side-by-side

# left panel: RTs in permuted and random material groups
apa_lineplot(
  data = tmp
  , id = "id"
  , dv = "SRI"
  , factors = c("block", "material")
  , dispersion = wsci
  , ylab = "Mean RT [ms]"
  , args_lines = list(lty = c("solid", "dotted"))
  , args_points = list(pch = c(21, 23), bg = c("grey70", "white"))
  , args_legend = list(legend = c("Permuted", "Random"))
  , ylim = c(475, 650)
  , jit = .05
)

# within the permuted-material group, high vs. low frequency locations can be distinguished
tmp.perm <- acquisition[
  acquisition$error == 0 &
  acquisition$trial > 1 &
  acquisition$included_participant & 
  acquisition$material == "Permuted" & 
  !is.na(acquisition$frequency),
]

apa_lineplot(
  data = tmp.perm
  , id = "id"
  , dv = "SRI"
  , factors = c("block", "frequency")
  , dispersion = wsci
  , ylab = ""
  , args_lines = list(lty = c("solid", "solid"))
  , args_points = list(pch = c(21, 21))
  , args_legend = list(legend = c("High", "Low"), title = "Location frequency")
  , ylim = c(475, 650)
  , jit = .05
)
```



Bonus

Bonus 1

Add a setup chunk at the top of your document. In this chunk

  1. load the knitr package and
  2. set the default chunk options to hide all messages and warnings.

You can insert a new code chunk by clicking or with the keyboard shortcut Ctrl + Alt + I.

See the documentation for knitr::opts_chunk.

Solution

The following should be placed at the very top of the document after the YAML front matter.

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

library("knitr")

opts_chunk$set(message = FALSE, warning = FALSE)
```



Bonus 2

To document the versions of all packages that you used for the analysis, let’s document your R session (operating system, R version, R packages, etc.).

Add a section named Computational environment at the end of your document and report your session information.
See the documentation for sessioninfo::session_info().

Solution

sessioninfo::session_info(dependencies = c("Depends", "Suggests"))
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.2.3 (2023-03-15)
##  os       macOS Ventura 13.1
##  system   x86_64, darwin22.3.0
##  ui       unknown
##  language (EN)
##  collate  en_US.UTF-8
##  ctype    en_US.UTF-8
##  tz       Europe/Berlin
##  date     2023-04-21
##  pandoc   3.1.2 @ /usr/local/bin/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package      * version    date (UTC) lib source
##  assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.2.1)
##  backports      1.4.1      2021-12-13 [1] CRAN (R 4.2.1)
##  base64url      1.4        2018-05-14 [1] CRAN (R 4.2.1)
##  bslib          0.4.1      2022-11-02 [1] CRAN (R 4.2.2)
##  cachem         1.0.6      2021-08-19 [1] CRAN (R 4.2.1)
##  callr          3.7.3      2022-11-02 [1] CRAN (R 4.2.2)
##  cli            3.6.0      2023-01-09 [1] CRAN (R 4.2.2)
##  codetools      0.2-19     2023-02-01 [2] CRAN (R 4.2.3)
##  data.table     1.14.6     2022-11-16 [1] CRAN (R 4.2.2)
##  digest         0.6.31     2022-12-11 [1] CRAN (R 4.2.2)
##  dplyr          1.1.0      2023-01-29 [1] CRAN (R 4.2.2)
##  evaluate       0.18       2022-11-07 [1] CRAN (R 4.2.2)
##  fansi          1.0.3      2022-03-24 [1] CRAN (R 4.2.1)
##  fastmap        1.1.0      2021-01-25 [1] CRAN (R 4.2.1)
##  fs           * 1.5.2      2021-12-08 [1] CRAN (R 4.2.1)
##  furrr          0.3.1      2022-08-15 [1] CRAN (R 4.2.2)
##  future         1.28.0     2022-09-02 [1] CRAN (R 4.2.1)
##  future.callr   0.8.1      2022-12-14 [1] CRAN (R 4.2.2)
##  generics       0.1.3      2022-07-05 [1] CRAN (R 4.2.1)
##  globals        0.16.1     2022-08-28 [1] CRAN (R 4.2.1)
##  glue           1.6.2      2022-02-24 [1] CRAN (R 4.2.1)
##  htmltools      0.5.3      2022-07-18 [1] CRAN (R 4.2.1)
##  igraph         1.3.5      2022-09-22 [1] CRAN (R 4.2.1)
##  jquerylib      0.1.4      2021-04-26 [1] CRAN (R 4.2.1)
##  jsonlite       1.8.4      2022-12-06 [1] CRAN (R 4.2.2)
##  klippy         0.0.0.9500 2022-10-17 [1] Github (rlesur/klippy@378c247)
##  knitr          1.41       2022-11-18 [1] CRAN (R 4.2.2)
##  lifecycle      1.0.3      2022-10-07 [1] CRAN (R 4.2.1)
##  listenv        0.8.0      2019-12-05 [1] CRAN (R 4.2.1)
##  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.2.1)
##  parallelly     1.32.1     2022-07-21 [1] CRAN (R 4.2.1)
##  pillar         1.8.1      2022-08-19 [1] CRAN (R 4.2.1)
##  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.2.1)
##  processx       3.8.0      2022-10-26 [1] CRAN (R 4.2.2)
##  ps             1.7.2      2022-10-26 [1] CRAN (R 4.2.2)
##  purrr          0.3.5      2022-10-06 [1] CRAN (R 4.2.2)
##  R6             2.5.1      2021-08-19 [1] CRAN (R 4.2.1)
##  rlang          1.0.6      2022-09-24 [1] CRAN (R 4.2.1)
##  rmarkdown    * 2.18       2022-11-09 [1] CRAN (R 4.2.2)
##  rprojroot    * 2.0.3      2022-04-02 [1] CRAN (R 4.2.1)
##  rstudioapi     0.14       2022-08-22 [1] CRAN (R 4.2.1)
##  sass           0.4.4      2022-11-24 [1] CRAN (R 4.2.2)
##  sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.2.1)
##  stringi        1.7.8      2022-07-11 [1] CRAN (R 4.2.1)
##  stringr        1.5.0      2022-12-02 [1] CRAN (R 4.2.2)
##  tarchetypes  * 0.7.4      2023-01-06 [1] CRAN (R 4.2.2)
##  targets      * 0.14.2     2023-01-06 [1] CRAN (R 4.2.2)
##  tibble         3.1.8      2022-07-22 [1] CRAN (R 4.2.1)
##  tidyselect     1.2.0      2022-10-10 [1] CRAN (R 4.2.2)
##  unilur         0.4.0.9100 2022-10-18 [1] Github (koncina/unilur@be7f93d)
##  utf8           1.2.2      2021-07-24 [1] CRAN (R 4.2.1)
##  vctrs          0.5.2      2023-01-23 [1] CRAN (R 4.2.2)
##  withr          2.5.0      2022-03-03 [1] CRAN (R 4.2.1)
##  xfun           0.35       2022-11-16 [1] CRAN (R 4.2.2)
##  yaml           2.3.6      2022-10-18 [1] CRAN (R 4.2.1)
## 
##  [1] /usr/local/lib/R/4.2/site-library
##  [2] /usr/local/Cellar/r/4.2.3/lib/R/library
## 
## ──────────────────────────────────────────────────────────────────────────────



Bonus 3

Create a brief presentation of your findings. Use R Markdown to either create

There are many great examples which you can adapt online.

xaringan builds on a powerful JavaScript library called remark.js. This library has many powerful advanced features. If this is something you are interested in, I recommend the following resources:

Solutions

After you have tried yourself, feel free to have a look at our solutions.