Installing the package
You can install the latest version of mpathsenser from CRAN:
install.packages("mpathsenser")Or the development version from Github:
remotes::install_github("koenniem/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 filesTo 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, .progress = FALSE)
#> 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 -1We 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 Activity AirQuality AppUsage
#> 48352 2 0 386
#> Battery Bluetooth Calendar Connectivity
#> 0 1103 98 35
#> Device Error GarminAccelerometer GarminActigraphy
#> 12 1 0 0
#> GarminBBI GarminEnhancedBBI GarminGyroscope GarminHeartRate
#> 0 0 0 0
#> GarminMeta GarminRespiration GarminSkinTemperature GarminSPO2
#> 0 0 0 0
#> GarminSteps GarminStress GarminWristStatus GarminZeroCrossing
#> 0 0 0 0
#> Geofence Gyroscope Heartbeat InstalledApps
#> 0 18542 0 1224
#> Keyboard Light Location Memory
#> 0 538 37 84
#> Mobility Noise Pedometer PhoneLog
#> 0 0 2511 0
#> Screen TextMessage Timezone Weather
#> 332 0 0 35
#> Wifi
#> 84Now 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.51.0 [C:\Users\u0134047\AppData\Local\Temp\RtmpIrwLkU\readme\some_db.db]
#> measurement_id participant_id date time step_count
#> <chr> <chr> <chr> <chr> <int>
#> 1 fd1a1310-ebc2-11ec-9b71-3ba318669196 2784 2022-06-14 09:18:44 118532
#> 2 d1847900-ebc4-11ec-879d-b5caf642e57d 2784 2022-06-14 09:31:50 118540
#> 3 e6214460-ebc4-11ec-a49f-29d5cd426d5e 2784 2022-06-14 09:32:24 118546
#> 4 e6982210-ebc4-11ec-adf0-bd5a8a64c663 2784 2022-06-14 09:32:25 118547
#> 5 e70c1990-ebc4-11ec-b4e1-7bbba2cce9b4 2784 2022-06-14 09:32:26 118548
#> # ℹ more rowsA 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.
