Learn R Programming

mpathsenser

Installing the package

You can install the latest version of mpathsenser from CRAN:

install.packages("mpathsenser")

Alternatively, you can install the development version from my Gitlab repo. First, make sure you have Rtools (Windows, Linux) or XCode installed. For XCode, register as an Apple Developer (don’t worry, it’s free) and then run xcode-select --install in a terminal. Then, run the following code in R:

devtools::install_git("https://gitlab.kuleuven.be/ppw-okpiv/researchers/u0134047/mpathsenser")

Importing files

Specify a path variable to wherever you put the JSON files. Make sure to use / and not a backslash.

path <- "~/Mobile Sensing Study/Data"

If you haven’t done so, unzip all files.

unzip_data(path = path)
#> Unzipped 37 files.

In m-Path Sense, data is written to JSON files as it comes in. In the JSON file format, every file starts with [ and ends with ]. If the app is killed, JSON files may not be properly closed and hence cannot be read by JSON parsers. So, we must first test if all files are in a valid JSON format and fix those that are not.

# Test JSONs for problems. Output is a character vector containing bad files (if any).
to_fix <- test_jsons(path = path)
#> Warning: There were issues in some files

# Fix JSON files if there are any.
# Note that test_jsons() returns the full path names, so a path directory is not necessary.
if (length(to_fix) > 0) {
  fix_jsons(path = NULL, files = to_fix)
}
#> Fixed 12 files

To import data, first create a database.

db <- create_db(path = path, db_name = "some_db.db")

Then, call import() to start reading in the files.

import(path = path, db = db)
#> All files were successfully written to the database.

If everything went correctly, there should be a message that all files were successfully written to the database. Otherwise import() return a character vector containing the files that failed to be imported. Note that files only need to be imported once, and that new files can be added to the database by calling import() again using the same database. Files that were processed previously will be skipped.

Extracting data from the database

Once files are imported, you can establish a database connection with open_db(). Don’t forget to save it to a variable!

db <- open_db(
  path = path, 
  db_name = "some_db.db"
)

To find out which participants are in the database (or rather their participant numbers):

get_participants(db)
#>   participant_id     study_id
#> 1           2784 Study_Merijn
#> 2            N/A           -1

We can also check what device they are using (which can be found in the Device table of the database).

device_info(db = db)
#> # A tibble: 1 × 10
#>   participant_id device_id    hardware device_name device_manufacturer device_model operating_system
#>   <chr>          <chr>        <chr>    <chr>       <chr>               <chr>        <chr>           
#> 1 2784           SP1A.210812… qcom     r8q         samsung             SM-G780G     REL             
#> # ℹ 3 more variables: platform <chr>, operating_system_version <chr>, sdk <chr>

To find out how much data there is in this database, look at the number of rows as an indication. Note that this operation may be slow for large databases, as every tables in the database needs to be queried.

get_nrows(db)
#> Accelerometer    AirQuality      Activity      AppUsage       Battery     Bluetooth      Calendar 
#>         75680             0             2           386             0          1103            98 
#>  Connectivity        Device         Error      Geofence     Gyroscope     Heartbeat InstalledApps 
#>            40            12             1             0         26509             0          1236 
#>      Keyboard         Light      Location        Memory      Mobility         Noise     Pedometer 
#>             0           538            37            84             0             0          4099 
#>      PhoneLog        Screen   TextMessage      Timezone       Weather          Wifi 
#>             0           358             0             0            35            84

Now let’s find out how to actually retrieve data from the database. There is a simple function for this, which is called get_data(). With this function you can extract any kind of data you want. Make sure you also run ?get_data for an overview of how to use this (or any other) function. In most functions, you can also leave arguments empty to retrieve all data (e.g. not in a specific time window).

get_data(
  db = db, # the ACTIVE database connection, open with open_db AND save to a variable
  sensor = "Pedometer", # A sensor name, see mpathsenser::sensors for the full list
  participant_id = "2784", # A participant ID, see get_participants
  start_date = "2022-06-14", # An optional start date, in the format YYYY-MM-DD
  end_date = "2022-06-15" # An optional end date, in the format YYYY-MM-DD
)
#> # Source:   SQL [?? x 5]
#> # Database: sqlite 3.50.4 [C:\Users\u0134047\AppData\Local\Temp\RtmpiQPrJV\readme\some_db.db]
#>   measurement_id                       participant_id date       time     step_count
#>   <chr>                                <chr>          <chr>      <chr>         <int>
#> 1 ce16d410-ebc5-11ec-a276-bfb1e065589a 2784           2022-06-14 09:38:54     119131
#> 2 ce659050-ebc5-11ec-a235-b1fd6433d9e2 2784           2022-06-14 09:38:54     119132
#> 3 ceb64860-ebc5-11ec-8f07-93c1927f71b2 2784           2022-06-14 09:38:55     119133
#> 4 cf133570-ebc5-11ec-bf61-85f33d53f14d 2784           2022-06-14 09:38:55     119134
#> 5 cfb47e80-ebc5-11ec-b17b-85bcc3b36c13 2784           2022-06-14 09:38:56     119136
#> # ℹ more rows

A more comprehensive guide is provided in the Get Started vignette.

Reference

For an overview of all functions in this package, see the mpathsenser Reference Site. The database schema used in this package can be found here.

Getting help

If you encounter a clear bug or need help getting a function to run, please file an issue with a minimal reproducible example on Gitlab.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Copy Link

Version

Install

install.packages('mpathsenser')

Monthly Downloads

296

Version

1.2.4

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Koen Niemeijer

Last Published

November 13th, 2025

Functions in mpathsenser (1.2.4)

get_studies

Get all studies
index_db

Create indexes for an mpathsenser database
import

Import m-Path Sense files into a database
identify_gaps

Identify gaps in mpathsenser mobile sensing data
geocode_rev

Reverse geocoding with latitude and longitude
get_participants

Get all participants
get_processed_files

Get all processed files from a database
screen_duration

Screen duration by hour or day
last_date

Extract the date of the last entry
n_screen_unlocks

Get number of screen unlocks
n_screen_on

Get number of times screen turned on
sensors

Available Sensors
link

Link y to the time scale of x
link_db

Link two sensors OR one sensor and an external data frame using an mpathsenser database
moving_average

Moving average for values in an mpathsenser database
installed_apps

Get installed apps
link_gaps

Link gaps to (ESM) data
mpathsenser-package

mpathsenser: Process and Analyse Data from m-Path Sense
vacuum_db

Vacuum a database
with_localtime

Convert timestamps to UTC while respecting local timezones
test_jsons

Test JSON files for being in the correct format.
step_count

Get step count
unpack_sensor_data

Unpack raw sensor data
open_db

Open an mpathsenser database.
haversine

Calculate the Great-Circle Distance between two points in kilometers
unzip_data

Unzip m-Path Sense output
plot.coverage

Plot a coverage overview
app_category

Find the category of an app on the Google Play Store
add_gaps

Add gap periods to sensor data
alias_column_names

Alias column names of a data frame containing sensor data
bin_data

Create bins in variable time series
copy_db

Copy (a subset of) a database to another database
ccopy

Copy mpathsenser zip files to a new location
close_db

Close a database connection
add_timezones_to_db

Add timezone to measurements in an m-Path Sense database
get_data

Extract data from an m-Path Sense database
get_nrows

Get the number of rows per sensor in a mpathsenser database
device_info

Get the device info for one or more participants
coverage

Create a coverage chart of the sampling rate
first_date

Extract the date of the first entry
app_usage

Get app usage per hour
freq

Measurement frequencies per sensor
activity_duration

Get a summary of physical activity (recognition)
decrypt_gps

Decrypt GPS data from a curve25519 public key
create_db

Create a new mpathsenser database
fix_jsons

Fix the end of JSON files