Basic Setup

The moodleR package contains functions and algorithms to access Moodle\(^{TM}\) data and perform reporting and/or learning analytics based in these data. For more information about Moodle go to For more information about \(\chi^\color{red}2\) (chi square) Laboratories and the work we do in learning analytics, visit our website:

Installing and loading the package

To install the latest stable release from CRAN use:


To install the latest development version:


After which the package is attached in the usual way.


Connecting to a Moodle Database


When connecting to a moodle database moodleR will look for database information in a config file (config.yml). The fields that need to be included can be seen in the snipped below:

    user: "root"
    password: "root"
    dbname: "moodle38"
    host: "localhost"
    port: 3306

Connecting to a moodle DB instance

Once the connection information available you can get a connection to the database by calling mdl_get_connection(), with the use_cache set to false:

mdl_con <- mdl_get_connection(use_cache = FALSE)

The reason for use_cache = FALSE will be explained below.

Caching Data

The moodleR package allows you to create a cache of the most relevant data on your local computer.

There are several reasons you would want to cache data locally rather than operating directly on a moodle database:

The moodleR package provides a method moodle_cache which allows for local caching of the data.

Caching to sqlite

When you run the moodle_cache function moodleR will create a local directory mdl_cache in your working directory. In this directory an sqlite database, mdl_cache.sqlite, will be created to contain the cached tables.


The output should look something like this:

#> Gettng Moodle DB connection
#> Gettng sqlite connection
#> Directory ' /home/sasha/chi2/moodleR/mdl_cache' used by default. 
#> Set config variable in moodleR:mdl_cache_dir to override.
#> This message is displayed once per session.
#> Filename 'mdl_cache.sqlite' used by default. 
#> Set config variable in moodleR:mdl_cache_dir to override.
#> Cache file created: /home/sasha/chi2/moodleR/mdl_cache/mdl_cache.sqlite
#> Downloading course table
#> Joining, by = "categoryid"
#> Caching course table
#> Downloading discussion posts table
#> Caching discussion posts table
#> Downloading user table
#> Caching user table
#> Downloading grades table
#> Caching grades table
#> Downloading config table
#> Downloading log table
#> Caching log table
#> Downloading roles table
#> Caching role table
#> Downloading enrolments
#> Caching enrolments