matchServer: Matching Game Server Function

View source: R/matchServer.R

matchServerR Documentation

Matching Game Server Function

Description

Matching Game Server Function

Usage

matchServer(
  id = "game",
  triggerInit,
  triggerReturn,
  startVal = 1,
  result = c("hide", "disable"),
  n2find,
  n_items = 24,
  n_cols = 4,
  items = "default",
  lab_type = c("icon", "text"),
  randomTarget = TRUE,
  randomGrid = FALSE,
  size = 2,
  color = "default",
  fill = "default"
)

Arguments

id

The input ID associated with the matching game module. Must match the ID of matchUI().

triggerInit

The reactive expression that triggers the initial appearance of the matching game. Must be created with or wrapped in reactive().

triggerReturn

The reactive expression that triggers the reappearance of the matching game UI. Must be created with or wrapped in reactive().

startVal

Integer or reactive expression that returns an integer. Default = 1. The participant's trial number when entering the game for this session. Useful if the participant resumes a game from a previous session. If using a reactive expression, should be wrapped in reactive().

result

A character value describing what should happen when the participant finds a match. Must be either "disable" or "hide".

n2find

Integer. How many items must the participant find in total?

n_items

Integer. How many items should be displayed? Should be evenly divisible by n_cols

n_cols

Integer. How many columns should the grid have?

items

Character. A list of items (either Font Awesome icons) or words to use as labels for the buttons. Default select a random sample of length n_items from a list of 100 free Font Awesome icons. If not using "default", vector must be the same length as n_items.

lab_type

Are the items text labels or icons? Must be either "text" or "icon"

randomTarget

Boolean. Should the targets be selected from the list randomly (TRUE) or in a particular order (FALSE)?

randomGrid

Boolean. Should the grid of items be randomized for each participant?

size

An integer between 1 and 3 indicating the size of the matching game buttons (larger = bigger)

color

A single hex code or vector of hex codes indicating the color of the icons or text. Default is black.

fill

A single hex code or vector of hex codes indicating the fill of the matching game buttons. Defaults to values from the "Bright" colorblind friendly palette from Paul Tol (see link in references below).

Value

A reactive list with 4 items: (1) score = the number of matches the participant has found so far, (2) i_df = a data.frame containing the list of items used in the present game and their order, (3) time_df: a data frame containing the start and stop times for each trial and the difference between the two in milliseconds, and (4) a reactive value indicating whether the participant's answer is correct (1) or incorrect (0).

References

Paul Tol's colorblind-friendly palettes (the source of the default button colors) can be found at https://personal.sron.nl/~pault/#sec:qualitative.

See Also

Must be used with matchUI.

Examples

library(shiny)
library(shinyjs)

# For the sake of a short example, we'll only require 3 matches.
ui <- fluidPage(
  fluidRow(
    column(width = 8, offset = 2,
           actionButton("start", "Start"),
           hidden(actionButton("again", "Play Again")),
           matchUI()
           )
  )
)

server <- function(input, output, session) {
  matches <- matchServer(triggerInit = reactive(input$start),
              triggerReturn = reactive(input$again),
              n2find = 3,
              randomGrid = TRUE,
              lab_type = "icon",
              result = "hide")

  observe({
    if (matches()$n_found > 0) {
      showElement("again")
    }
  })
}

shinyApp(ui = ui, server = server)


abbey-thomas/speechcollectr documentation built on Nov. 19, 2024, 7:09 p.m.