## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rquiz)

## ----eval = FALSE-------------------------------------------------------------
# myQuestionSC <- list(
#   question = "Which city hosted the 2024 Summer Olympics?", # the question text
#   options = c("Berlin", "London", "Paris", "Madrid"),  # answer options
#   answer = 3,  # index of the correct answer (Paris)
#   tip = "Think about the Eiffel Tower."  # optional tip text
# )

## ----eval = FALSE-------------------------------------------------------------
# myQuestionMC <- list(
#   question = "Which of the following are prime numbers?",
#   options = c("2", "4", "7", "9", "11", "15"),
#   answer = c(1, 3, 5),  # indices of correct options (2, 7, 11)
#   tip = "Prime numbers are only divisible by 1 and themselves."
# )

## ----eval = FALSE-------------------------------------------------------------
# checkSingleQuestion(myQuestionSC)  # returns invisibly if valid
# 
# # Common mistake: unnamed or partially named list
# bad <- list("What is 2+2?", c("3", "4"), answer = 2)
# checkSingleQuestion(bad)
# #> Error: The question list must have named elements: 'question', 'options',
# #>   and 'answer' (plus optionally 'tip'). Missing: question, options.
# #>   Example: list(question = "What is 2+2?", options = c("3", "4", "5"),
# #>   answer = 2)
# #>   See ?checkSingleQuestion or ?singleQuestion for examples.

## ----eval = FALSE-------------------------------------------------------------
# sportQuiz <- list(
#   q1 = list(
#     question = "Which city hosted the 2024 Summer Olympics?",
#     options = c("Athens", "London", "Paris", "Tokyo"),
#     answer = 3
#   ),
#   q2 = list(
#     question = "Which sport was added to the Olympics at the 2024 Paris Games?",
#     options = c("Skateboarding", "Breakdance", "Surfing", "Sport Climbing"),
#     answer = 2
#   ),
#   q3 = list(
#     question = "At the 1900 Olympics in Paris, one sport was featured that has never returned to the Games since. Which one?",
#     options = c("Live pigeon shooting", "Underwater archery", "Synchronized sneezing",
#       "Backwards horse vaulting"),
#     answer = 1
#   )
# )

## ----eval = FALSE-------------------------------------------------------------
# sportQuizWithTips <- list(
#   q1 = list(
#     question = "Which city hosted the 2024 Summer Olympics?",
#     options = c("Athens", "London", "Paris", "Tokyo"),
#     answer = 3,
#     tip = "They speak French there."
#   ),
#   q2 = list(
#     question = "Which sport was added to the Olympics at the 2024 Paris Games?",
#     options = c("Skateboarding", "Breakdance", "Surfing", "Sport Climbing"),
#     answer = 2
#     # no tip for this question - the tip button won't appear here
#   )
# )

## ----eval = FALSE-------------------------------------------------------------
# scienceQuiz <- list(
#   q1 = list(
#     question = "Which of these are noble gases?",
#     options = c("Helium", "Oxygen", "Neon", "Iron"),
#     answer = c(1, 3)
#   ),
#   q2 = list(
#     question = "Which planet is closest to the Sun?",
#     options = c("Venus", "Mercury", "Mars"),
#     answer = 2  # single correct answer, but rendered as checkbox
#   )
# )

## ----eval = FALSE-------------------------------------------------------------
# checkMultiQuestions(sportQuiz)  # returns invisibly if valid
# 
# # Common mistake: flat list instead of list of lists
# bad <- list(question = "Q1?", options = c("A", "B"), answer = 1,
#             question = "Q2?", options = c("C", "D"), answer = 2)
# checkMultiQuestions(bad)
# #> Error: `x` looks like a single question, not a list of questions.
# #>   Wrap it in an outer list: list(q1 = list(question = ..., options = ...,
# #>   answer = ...))
# #>   See ?checkMultiQuestions or ?multiQuestions for examples.

## ----eval = FALSE-------------------------------------------------------------
# rQuiz <- list(
#   cloze = "R is a $$!programming!$$ language for $$!statistical computing!$$.",
#   addOptions = c("natural", "colloquial", "movies")
# )

## ----eval = FALSE-------------------------------------------------------------
# codeQuiz <- list(
#   cloze = "<pre>x $$!<-!$$ c(1, 2, 3)
# $$!mean!$$(x)</pre>",
#   addOptions = c("=", "->", "median", "sum")
# )

## ----eval = FALSE-------------------------------------------------------------
# checkFillBlanks(rQuiz)  # returns invisibly if valid
# 
# # Common mistake: misspelled element name
# bad <- list(clozed = "R is a $$!programming!$$ language.")
# checkFillBlanks(bad)
# #> Error: Unknown element(s) in `x`: 'clozed'.
# #>   Allowed elements are: 'cloze' (required) and 'addOptions' (optional).
# #>   See ?checkFillBlanks or ?fillBlanks for examples.

## -----------------------------------------------------------------------------
myQuestionSC <- list(
  question = "Which city hosted the 2024 Summer Olympics?", # the question text
  options = c("Berlin", "London", "Paris", "Madrid"),  # answer options
  answer = 3,  # index of the correct answer (Paris)
  tip = "Think about the Eiffel Tower."  # optional tip text
)
singleQuestion(
  x = myQuestionSC,
  title = "Geography Quiz",
  showTipButton = TRUE
)

## ----eval = FALSE-------------------------------------------------------------
# # Define a dark theme once
# dark <- rquizTheme(
#   fontFamily = "Georgia, serif",
#   fontSize = 18,
#   titleCol = "#E0E0E0", titleBg = "#1A1A2E",
#   questionCol = "#FFFFFF", questionBg = "#16213E",
#   mainCol = "#E0E0E0", mainBg = "#1A1B2E",
#   optionBg = "#252540",
#   navButtonCol = "#FFFFFF", navButtonBg = "#E94560",
#   tipButtonCol = "#E0E0E0", tipButtonBg = "#2C2C3E",
#   solutionButtonCol = "#E0E0E0", solutionButtonBg = "#2C2C3E"
# )
# 
# # Apply to any quiz - all share the same design:
# singleQuestion(x = myQuestionSC, theme = dark, title = "Quiz 1")
# multiQuestions(x = scienceQuiz, theme = dark, title = "Quiz 2")
# fillBlanks(x = rQuiz, theme = dark, title = "Quiz 3")

## ----eval = FALSE-------------------------------------------------------------
# # Use the dark theme but override the title background:
# singleQuestion(x = myQuestionSC, theme = dark, title = "Quiz 1",
#   titleBg = "#FF0000")

## ----eval = FALSE-------------------------------------------------------------
# list(
#   question = "What is the scientific name of the blue whale?",
#   options = c(
#     "<em>Balaenoptera musculus</em>",
#     "<em>Megaptera novaeangliae</em>",
#     "<em>Physeter macrocephalus</em>"
#   ),
#   answer = 1
# )

## ----eval = FALSE-------------------------------------------------------------
# list(
#   question = "What does the pipe operator <code>|></code> do in R?",
#   options = c(
#     "It performs logical OR operations",
#     "It passes the left side as the first argument to the right side",
#     "It creates a new variable"
#   ),
#   answer = 2
# )

## ----eval = FALSE-------------------------------------------------------------
# list(
#   question = "Which <span style='color: #E74C3C;'>color</span> does litmus paper turn in an acidic solution?",
#   options = c("Blue", "Red", "Green", "Yellow"),
#   answer = 2
# )

## ----eval = FALSE-------------------------------------------------------------
# list(
#   cloze = "<pre>library($$!ggplot2!$$)<br>
# ggplot(data = iris) +<br>
#   $$!geom_point!$$(aes(x = Sepal.Length, y = Sepal.Width))</pre>",
#   addOptions = c("geom_line", "dplyr")
# )

## ----eval = FALSE-------------------------------------------------------------
# # German
# singleQuestion(x = myQuestionSC, language = "de")
# 
# # French
# multiQuestions(x = sportQuiz, language = "fr")
# 
# # Spanish
# fillBlanks(x = rQuiz, language = "es")

## ----eval = FALSE-------------------------------------------------------------
# singleQuestion(x = myQuestionSC, scroll = TRUE, height = "400px")

## ----eval = FALSE-------------------------------------------------------------
# library(shiny)
# library(rquiz)
# 
# ui <- fluidPage(
#   h2("My Quiz App"),
#   singleQuestionOutput("quiz")
# )
# 
# server <- function(input, output) {
#   output$quiz <- renderSingleQuestion({
#     singleQuestion(
#       x = list(
#         question = "What is 2+2?",
#         options = c("3", "4", "5"),
#         answer = 2
#       )
#     )
#   })
# }
# 
# shinyApp(ui, server)

