inst/shiny-exams/examsapp/global.R

## global setup file ##

############# Support Functions ###############################
get_template_code <- function(type, markup)
{
  if(markup == "LaTeX") {
    excode <- switch(type,
                     "schoice" = c('<<echo=FALSE, results=hide>>=',
                                   '## DATA GENERATION EXAMPLE',
                                   'cities <- c("Munich", "Innsbruck", "Zurich", "Amsterdam")',
                                   'countries <- c("Germany", "Austria", "Switzerland", "Netherlands")',
                                   'question <- sample(cities, size = 1)',
                                   '@',
                                   '',
                                   '\\begin{question}',
                                   '%% Enter the question here, you can access R variables with \\Sexpr{},',
                                   '%% e.g., \\Sexpr{question} will return the name of the sampled city in the code above.',
                                   'In which country is Munich?',
                                   '\\begin{answerlist}',
                                   '  \\item Austria',
                                   '  \\item Germany',
                                   '  \\item Switzerland',
                                   '  \\item Netherlands',
                                   '\\end{answerlist}',
                                   '\\end{question}',
                                   '',
                                   '\\begin{solution}',
                                   '%% Supply a solution here!',
                                   'Munich is in Germany.',
                                   '\\begin{answerlist}',
                                   '  \\item False.',
                                   '  \\item True.',
                                   '  \\item False.',
                                   '  \\item False.',
                                   '\\end{answerlist}',
                                   '\\end{solution}',
                                   '',
                                   '%% META-INFORMATION',
                                   '%% \\extype{schoice}',
                                   '%% \\exsolution{0100}',
                                   '%% \\exname{Mean}',
                                   '%% \\exshuffle{Cities}'),
                     "num" = c('<<echo=FALSE, results=hide>>=',
                               '## DATA GENERATION EXAMPLE',
                               'x <- c(-0.17, 0.63, 0.96, 0.97, -0.77)',
                               'Mean <- mean(x)',
                               '@',
                               '',
                               '\\begin{question}',
                               '%% Enter the question here, you can access R variables with \\Sexpr{},',
                               '%% e.g., \\Sexpr{Mean} will return the mean of variable x in the R code above.',
                               'Calculate the mean of the following numbers: \\\\',
                               '$',
                               '-0.17, 0.63, 0.96, 0.97, -0.77.',
                               '$',
                               '\\end{question}',
                               '',
                               '\\begin{solution}',
                               '%% Supply a solution here!',
                               'The mean is $0.324$.',
                               '\\end{solution}',
                               '',
                               '%% META-INFORMATION',
                               '%% \\extype{num}',
                               '%% \\exsolution{0.324}',
                               '%% \\exname{Mean}',
                               '%% \\extol{0.01}'),
                     "mchoice" = c('<<echo=FALSE, results=hide>>=',
                                   '## DATA GENERATION EXAMPLE',
                                   'x <- c(33, 3, 33, 333)',
                                   'y <- c(3, 3, 1/6, 1/33.3)',
                                   'solutions <- x * y',
                                   '@',
                                   '',
                                   '\\begin{question}',
                                   '%% Enter the question here, you can access R variables with \\Sexpr{},',
                                   '%% e.g., \\Sexpr{solutions[1]} will return the solution of the first statement.',
                                   'Which of the following statements is correct?',
                                   '\\begin{answerlist}',
                                   '  \\item $33 \\cdot 3 = 109$',
                                   '  \\item $3 \\cdot 3 = 9$',
                                   '  \\item $33 / 6 = 5.5$',
                                   '  \\item $333 / 33.3 = 9$',
                                   '\\end{answerlist}',
                                   '\\end{question}',
                                   '',
                                   '\\begin{solution}',
                                   '%% Supply a solution here!', '',
                                   '\\begin{answerlist}',
                                   '  \\item False. Correct answer is $33 \\cdot 3 = 99$.',
                                   '  \\item True.',
                                   '  \\item True.',
                                   '  \\item False. Correct answer is $333 / 33.3 = 10$.',
                                   '\\end{answerlist}',
                                   '\\end{solution}',
                                   '',
                                   '%% META-INFORMATION',
                                   '%% \\extype{mchoice}',
                                   '%% \\exsolution{0110}',
                                   '%% \\exname{Simple math}',
                                   '%% \\exshuffle{TRUE}'),
                     "string" = c('<<echo=FALSE, results=hide>>=',
                                  '## DATA GENERATION EXAMPLE',
                                  'cities <- c("Munich", "Innsbruck", "Zurich", "Amsterdam")',
                                  'countries <- c("Germany", "Austria", "Switzerland", "Netherlands")',
                                  'question <- sample(cities, size = 1)',
                                  '@',
                                  '',
                                  '\\begin{question}',
                                  '%% Enter the question here, you can access R variables with \\Sexpr{},',
                                  '%% e.g., \\Sexpr{question} will return the name of the sampled city in the code above.',
                                  'In which country is Munich?',
                                  '\\end{question}',
                                  '',
                                  '\\begin{solution}',
                                  '%% Supply a solution here!',
                                  'Munich is in Germany.',
                                  '\\end{solution}',
                                  '',
                                  '%% META-INFORMATION',
                                  '%% \\extype{string}',
                                  '%% \\exsolution{Germany}',
                                  '%% \\exname{Cities 2}'),
                     "cloze" = c('<<echo=FALSE, results=hide>>=',
                                 '## DATA GENERATION EXAMPLE',
                                 'x <- c(-0.17, 0.63, 0.96, 0.97, -0.77)',
                                 'Mean <- mean(x)',
                                 'Sd <- sd(x)',
                                 'Var <- var(x)',
                                 '@',
                                 '',
                                 '\\begin{question}',
                                 '%% Enter the question here, you can access R variables with \\Sexpr{},',
                                 '%% e.g., \\Sexpr{Mean} will return the mean of variable x in the R code above.',
                                 'Given the following numbers: \\\\',
                                 '$',
                                 '-0.17, 0.63, 0.96, 0.97, -0.77.',
                                 '$',
                                 '\\begin{answerlist}',
                                 '  \\item What is the mean?',
                                 '  \\item What is the standard deviation?',
                                 '  \\item What is the variance?',
                                 '\\end{answerlist}',
                                 '\\end{question}',
                                 '',
                                 '\\begin{solution}',
                                 '%% Supply a solution here!', '',
                                 '\\begin{answerlist}',
                                 '  \\item The mean is $0.324$.',
                                 '  \\item The standard deviation is $0.767515$.',
                                 '  \\item The variance is $0.58908$.',
                                 '\\end{answerlist}',
                                 '\\end{solution}',
                                 '',
                                 '%% META-INFORMATION',
                                 '%% \\extype{cloze}',
                                 '%% \\exsolution{0.324|0.767515|0.58908}',
                                 '%% \\exclozetype{num|num|num}',
                                 '%% \\exname{Statistics}',
                                 '%% \\extol{0.01}')
    )
  } else {
    excode <- switch(type,
                               "schoice" = c(''),
                               "num" = c('```{r data generation, echo = FALSE, results = "hide"}',
                                         '## DATA GENERATION',
                                         'n <- sample(50:150, 1)',
                                         'y <- rnorm(n, runif(1, 100, 200), runif(1, 10, 15))',
                                         '',
                                         '## QUESTION/ANSWER GENERATION',
                                         'Mean <- round(mean(y), digits = 1)',
                                         'Var <- round(var(y), digits = 1)',
                                         'sd <- sqrt(Var/n)',
                                         'LB <- round(Mean - 1.96*sd, 3)',
                                         'UB <- round(Mean + 1.96*sd, 3)',
                                         '```',
                                         '',
                                         'Question',
                                         '========',
                                         '',
                                         'The daily expenses of summer tourists in Vienna are analyzed. A',
                                         'survey with $`r n`$ tourists is conducted. This shows that the',
                                         'tourists spend on average $`r Mean`$ EUR. The sample variance',
                                         '$s^2_{n-1}$ is equal to $`r Var`$.',
                                         '',
                                         'Determine a $95$ confidence interval for the average daily',
                                         'expenses (in EUR) of a tourist.',
                                         '',
                                         '',
                                         'Solution',
                                         '========',
                                         '',
                                         'The $95$ confidence interval for the average expenses $\mu$ is',
                                         'given by:',
                                         '$$',
                                         '\begin{aligned}',
                                         '&   & \left[\bar{y} \, - \, 1.96\sqrt{\frac{s_{n-1}^2}{n}}, \;',
                                         '\bar{y} \, + \, 1.96\sqrt{\frac{s_{n-1}^2}{n}}\right] \\',
                                         '& = & \left[ `r Mean` \, - \, 1.96\sqrt{\frac{`r Var`}{`r n`}}, \;',
                                         '`r Mean` \, + \, 1.96\sqrt{\frac{`r Var`}{`r n`}}\right] \\',
                                         '& = & \left[`r LB`, \, `r UB`\right].',
                                         '\end{aligned}',
                                         '$$',
                                         '',
                                         '',
                                         'Meta-information',
                                         '============',
                                         'extype: num',
                                         'exsolution: `r LB`|`r UB`',
                                         'exname: Confidence interval',
                                         'extol: 0.01'),
                               "mchoice" = c(''),
                               "string" = c(''),
                               "cloze" = c('')
    )
  }

  excode
}

chooserInput <- function(inputId, leftChoices, rightChoices, size = 5, multiple = FALSE)
{
  leftChoices <- lapply(leftChoices, tags$option)
  rightChoices <- lapply(rightChoices, tags$option)
  questionNr <- lapply(if(length(rightChoices)) 1:length(rightChoices) else rightChoices, tags$li)

  if(multiple)
    multiple <- "multiple"
  else
    multiple <- NULL

  tagList(
    singleton(tags$head(
      tags$script(src="chooser-binding.js"),
      tags$style(type="text/css",
                 HTML(".chooser-container { display: inline-block; }")
      )
    )),
    div(id=inputId, class="chooser",
        div(class="chooser-container chooser-left-container",
            HTML("<b>Available</b><br>"),
            tags$select(class="left", size=size, multiple=multiple, leftChoices)
        ),
        div(class="chooser-container chooser-center-container",
            icon("arrow-circle-o-right", "right-arrow fa-2x"),
            tags$br(),
            icon("arrow-circle-o-left", "left-arrow fa-2x")
        ),
        div(class="chooser-container chooser-right-container",
            HTML("<b>Selected</b><br>"),
            tags$select(class="right", size=size, multiple=multiple, rightChoices)
        ),
        div(class="chooser-container chooser-question-container",
            HTML("<b>Question Nr.</b><br>"),
            tags$ul(class="question", size=size, multiple=multiple, questionNr)
        )
    )
  )
}
flaviobarros/shinyExams documentation built on May 21, 2019, 12:34 a.m.