Skip to contents

The canvasquiz package provides functions to interact with the Canvas LMS API, specifically for constructing quizzes and retrieving quiz data. This package allows you to write the quizzes in R and directly push them to Canvas, as well as retrieve quiz statistics and results for analysis.

Note that this package only supports Classic Quizzes in Canvas, not New Quizzes.

Installation

You can install the development version of canvasquiz from GitHub with:

# install.packages("pak")
pak::pak("emitanaka/canvasquiz")

Usage

Set environment variables

In the R environment, you can set the following environment variables to avoid having to specify them in each function call:

Sys.setenv(CANVASQUIZ_COURSE_ID = "your_course_id")
Sys.setenv(CANVASQUIZ_URL = "https://your_canvas_instance.instructure.com")
Sys.setenv(CANVASQUIZ_TOKEN = "your_access_token")

Or set this in your .Renviron file for persistence across sessions. You can use the usethis package to edit your .Renviron file:

usethis::edit_r_environ()

The URL is the base URL of your Canvas instance, e.g. at ANU it is https://canvas.anu.edu.au.

The access token can be generated in your Canvas account settings under “Approved Integrations”.

The course ID can be found in the URL of your course in Canvas. For example if your course URL is https://canvas.anu.edu.au/courses/12345, then the course ID is 12345.

Creating a quiz

qid <- create_quiz(title = "Quiz 1", 
                   description = "This assessment is 30 minutes with only one attempt.",
                   quiz_type = "practice_quiz",
                   attempts = attempt_options(n = 1, time = 30))

The output will be the quiz ID of the newly created quiz. Use this quiz ID to add questions to the quiz. If the quiz ID is not specified, the question will be added to the most recently created quiz.

Numerical question

The exact value match:

create_question("What is 2 + 2?",
                answers = answer_num(4),
                points = 1,
                quiz_id = qid)

The value match with a tolerance:

create_question("What is the square root of 2?",
                answers = answer_num(sqrt(2), tol = 0.1),
                points = 1,
                quiz_id = qid)

The value match with a specified number of decimal places:

create_question("What is the square root of 2?",
                answers = answer_num_precision(sqrt(2), precision = 1),
                points = 1,
                quiz_id = qid)

The value match within a specified range:

create_question("What is the value of pi?",
                answers = answer_num_range(3.14, 3.15),
                points = 1,
                quiz_id = qid)

Multiple choice question

A multiple choice question with one correct answer:

create_question("Which of the following are prime numbers?",
                answers = answer_mcq("2", choices = c("2", "10", "12")),
                quiz_id = qid)

A multiple choice question with multiple correct answers:

create_question("Which of the following are prime numbers?",
                answers = answer_mcq(c("2", "3", "5"), choices = c("2", "3", "4", "5")),
                quiz_id = qid)

True/False question

create_question("The Earth is flat.",
                answers = answer_true_false(FALSE),
                quiz_id = qid)

Short answer question

create_question("What is the capital of France?",
                answers = answer_text("Paris"),
                quiz_id = qid)

Essay question

create_question("Describe the process of photosynthesis.",
                answers = answer_essay(),
                quiz_id = qid)

Matching question

create_question("Match the following countries with their capitals.",
                answers = answer_matching(
                  left = c("France", "Japan", "Australia"),
                  right = c("Paris", "Tokyo", "Canberra"),
                  extra_choices = c("Madrid", "Beijing", "Wellington")
                ),
                quiz_id = qid)

Multiple answers

Selecting answers from multiple dropdowns:

create_question(
    "Roses are [color1], violets are [color2].",
    answers = answer_multiple(
      dropdown(
        choices = c("red", "blue", "yellow"),
        correct = "red",
        id = "color1"
      ),
      dropdown(
        choices = c("red", "blue", "yellow"),
        correct = "blue",
        id = "color2"
      )
    )
  )

Fill in the blanks:

create_question(
    "Roses are [color1], violets are [color2].",
    answers = answer_multiple(
      fill_in_the_blank("red", id = "color1"),
      fill_in_the_blank("blue", id = "color2")
    )
  )

Upload a file

create_question("Upload a file with your answer.",
                answers = answer_upload_file(),
                quiz_id = qid)

Writing quiz with mathematics

Writing LaTeX in Canvas quizzes via the API involves inserting LaTeX code within specific delimiters—\ and \ for inline, and and for display mode—directly into the HTML content fields of quiz questions. Canvas automatically renders this LaTeX via KaTeX, which supports standard mathematical notation.

create_question("What is the value of \\(\\pi\\) to two decimal places?",
                answers = answer_num_precision(3.14, precision = 2),
                quiz_id = qid)

Retrieving quiz data

Quiz statistics:

quiz_statistics(quiz_id = qid)

Quiz results with user information:

quiz_results(quiz_id = qid)

Question-level statistics:

quiz_submissions(quiz_id = qid)