2021-03-11 17:25:44 +01:00
# if you need a new package, you should add it with renv::install(package) so your renv venv is updated
library ( readr )
#' @description
#' Gets the sensor data for a specific device id from a database table, file or whatever source you want to query
#'
#' @param stream_parameters The PHONE_STREAM_PARAMETERS key in config.yaml. If you need specific parameters add them there.
#' @param device A device ID string
2021-03-13 00:14:49 +01:00
#' @param sensor_container database table or file containing the sensor data for all participants. This is the FITBIT_SENSOR[CONTAINER] key in config.yaml
2021-03-11 17:25:44 +01:00
#' @param columns the columns needed from this sensor (we recommend to only return these columns instead of every column in sensor_container)
#' @return A dataframe with the sensor data for device
pull_data <- function ( stream_parameters , device , sensor , sensor_container , columns ) {
if ( ! dir.exists ( stream_parameters $ FOLDER ) )
2021-03-11 18:19:44 +01:00
stop ( " [FITBIT_DATA_STREAMS][fitbitjson_csv][FOLDER] does not exist: " , stream_parameters $ FOLDER )
2021-03-11 17:25:44 +01:00
data_file <- file.path ( stream_parameters $ FOLDER , sensor_container )
if ( ! file.exists ( data_file ) )
stop ( " The data container should be a CSV file but it does not exist: " , data_file )
if ( ! endsWith ( data_file , " .csv" ) )
stop ( " The data container should be a CSV file: " , data_file )
# Letting the user know what we are doing
message ( paste0 ( " Reading this CSV file: " , data_file ) )
2021-03-12 01:32:11 +01:00
sensor_data <- read_delim_chunked ( data_file , escape_backslash = TRUE , delim = " ," , escape_double = FALSE , quote = " \"" ,
callback = DataFrameCallback $ new ( function ( x , pos ) x [x [ [columns $ DEVICE_ID ] ] == device , unlist ( columns , use.names = FALSE ) ] ) , progress = T , chunk_size = 50000 )
2021-03-11 17:25:44 +01:00
if ( is.null ( sensor_data ) ) # emtpy file
sensor_data <- read.csv ( data_file )
if ( nrow ( sensor_data ) == 0 )
warning ( " The device '" , device , " ' did not have data in " , sensor_container )
return ( sensor_data )
}