Learn R Programming

moodef

The goal of the moodef package is to support the definition of Moodle elements taking advantage of the power that R offers. In particular, in this first version, it allows the definition of questions to be included in the question bank to define quizzes.

To define the questions for the quizzes we can use the component for this purpose that includes Moodle, based on entering data through screens. It allows the import and export of questions in various formats, including XML.

Complementary, using the moodef package, we can define questionnaires from R. We have generalized 10 types of questions and simplified their definition. So, we define a question by calling a function or simply including a row in a data frame, a CSV file or an Excel file. The result is an XML file that we import into Moodle. If necessary, some parameter not considered in the generalization can be defined or adjusted there.

The package supports two styles of question definition: the simple style and the extended style. The following example is based on the simple style.

Although not shown in the next example, in each question we can include an image that is embedded in XML. We can set up the size of the images so that they are homogeneous when displayed in quizzes.

In addition to facilitating the definition of questions manually, the infrastructure offered by this package can be used to generate questions automatically or semi-automatically from R.

Installation

You can install the released version of when from CRAN with:

install.packages("moodef")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("josesamos/moodef")

Example

This is a basic example which shows the definition of a question using the function:

library(moodef)

qc <- question_category(category = 'Initial test',
                        copyright = 'Copyright © 2025 Universidad de Granada',
                        license = 'License Creative Commons Attribution-ShareAlike 4.0',
                        author = 'Jose Samos') |>
  define_question(
    question = 'What are the basic arithmetic operations?',
    answer = 'Addition, subtraction, multiplication and division.',
    a_1 = 'Addition and subtraction.',
    a_2 = 'Addition, subtraction, multiplication, division and square root.'
  )

file <- tempfile(fileext = '.xml')
qc <- qc |>
  generate_xml_file(file)

First, we create an object using the question_category function and configure general aspects of the definition in it. Next, we define the questions, as many as we need, using the define_question function (the type of the questions is deduced from the definition). Finally, we generate the questions in XML format, in the form of a string or file. We show the result below.

<?xml version="1.0" encoding="UTF-8"?>
<quiz>
  <question type="category">
    <category>
      <text>$course$/top/Initial test</text>
    </category>
    <info format="html">
      <text></text>
    </info>
    <idnumber></idnumber>
  </question>
  <question type="multichoice">
    <name>
      <text>q_001_multichoice_what_are_the_basic_arithmetic_operations</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[
        <!-- Copyright © 2025 Universidad de Granada -->
        <!-- License Creative Commons Attribution-ShareAlike 4.0 -->
        <!-- Author: Jose Samos -->
        <p>What are the basic arithmetic operations?</p>
      ]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1.0</defaultgrade>
    <penalty>0.5</penalty>
    <hidden>0</hidden>
    <idnumber></idnumber>
    <single>true</single>
    <shuffleanswers>true</shuffleanswers>
    <answernumbering>abc</answernumbering>
    <showstandardinstruction>0</showstandardinstruction>
    <correctfeedback format="moodle_auto_format">
      <text>Correct.</text>
    </correctfeedback>
    <partiallycorrectfeedback format="moodle_auto_format">
      <text>Partially correct.</text>
    </partiallycorrectfeedback>
    <incorrectfeedback format="moodle_auto_format">
      <text>Incorrect.</text>
    </incorrectfeedback>
    <answer fraction="100" format="html">
      <text>Addition, subtraction, multiplication and division.</text>
      <feedback format="html">
        <text>Correct.</text>
      </feedback>
    </answer>
    <answer fraction="0" format="html">
      <text>Addition and subtraction.</text>
      <feedback format="html">
        <text>Incorrect.</text>
      </feedback>
    </answer>
    <answer fraction="0" format="html">
      <text>Addition, subtraction, multiplication, division and square root.</text>
      <feedback format="html">
        <text>Incorrect.</text>
      </feedback>
    </answer>
  </question>
</quiz>

We can call the define_question function for each question to be defined, with the same parameters. Alternatively, we can create a data frame, a CSV file or an Excel file (with the functions available in the package) and include in them a row for each question, a column for each parameter. Below is the content of a CSV file in table format.

typequestionimageimage_altanswera_1a_2
What are the basic arithmetic operations?Addition, subtraction, multiplication and division.Addition and subtraction.Addition, subtraction, multiplication, division and square root.
Match each operation with its symbol.Addition<|>+Subtraction<|>-Multiplication<|>*
The square root is a basic arithmetic operation.False
What basic operation does it have as a + symbol?Addition
The symbol for addition is [[1]], the symbol for subtraction is [[2]].+-
xThe symbol for addition is [[1]], the symbol for subtraction is [[2]].+-
hSort the result from smallest to largest.6/26-26+2
vSort the result from smallest to largest.6/26-26+2
What is the result of SQRT(4)?2-2
What is the result of 4/3?1.33<|>0.03
Describe the addition operation.

The generation of the questions would be similar, as shown below.

file <- system.file("extdata", "questions.csv", package = "moodef")
qc <- question_category(category = 'Initial test',
                        copyright = 'Copyright © 2025 Universidad de Granada',
                        license = 'License Creative Commons Attribution-ShareAlike 4.0',
                        author = 'Jose Samos') |>
  define_questions_from_csv(file = file)

file <- tempfile(fileext = '.xml')
qc <- qc |>
  generate_xml_file(file)

We do not show the content of the XML file obtained because in that example there are several questions defined and it takes up a lot of space.

Copy Link

Version

Install

install.packages('moodef')

Monthly Downloads

152

Version

1.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Jose Samos

Last Published

January 26th, 2025

Functions in moodef (1.2.0)

generate_essay

Generate essay question
generate_xml

Generate questions xml string
generate_truefalse

Generate truefalse question
get_non_empty_fields_by_prefix

Get non-empty values from fields with a specific prefix
generate_question_body

Generate the question body
extract_type_orientation

Extract Type and Orientation
generate_ordering

Generate ordering question
process_question_dataframe

Process Question DataFrame
get_vector_answer

Convert Answer String to Vector
question_category

question_category S3 class
generate_xml_file

Generate questions xml file
xml_question_name

Define the question name in xml
xml_question_category

Define the question category in xml
generate_ddmarker

Generate ddmarker question
xml_question

Define the question in xml
get_detailed_type_names

Get Detailed Type Names
xml_image

Generate HTML and XML Representations for an Image
generate_question_name

generate question name node
generate_shortanswer

Generate shortanswer question
define_questions_from_df

Define questions from a data frame
xml_question_idnumber

Define the question id number in xml
filter_non_empty_answers

Filter non-empty answers
xml_question_tags

Define the question tags in xml
define_questions_from_excel

Define questions from a Excel file
generate_gapselect

Generate gapselect question
read_question_csv

Read a question csv file
define_questions_from_csv

Define questions from a csv file
define_questions_from_data_frame

Define questions from a data frame
read_question_excel

Read a question Excel file
generate_matching

Generate matching question
string_to_vector

Transform a String into a Vector of Strings
xml_questiontext

generate questiontext node
vector_to_string

Transforms a vector of strings into a string
simplified_types

Simplified Question Types
has_gaps

Check if a String Contains Gaps
is_numeric

Check if a String Represents a Numeric Value
string_to_string_vector

Transform a String into a String-Formatted Vector
generate_numerical

Generate numerical question
generate_multichoice

Generate multichoice question
create_default_value_question_df

Create a question data frame common to normal and extended questions with default values.
define_question

Define a question
create_question_excel

Create a question Excel file
create_question_csv

Create a question csv file
create_common_question_df

Create a question data frame common to normal and extended questions.
create_question_data_frame

Create a question data frame
define_extended_question

Define an extended question
adapt_image

Resize and Center an Image
category_question

Define the category of questions
determine_question_type

Determine question type
allowed_types

Allowed Question Types
generate_ddwtos

Generate ddwtos question