R/fct_helpers.R

Defines functions process

Documented in process

#' Process laser trap data
#' @description Takes the user created directions and either detrends or removes baseline
#' @param grouped_file a character vector specifyfing file path of observation folder
#' @param detrend logical
#' @param mv2nm numerical conversion factor between mV and nm
#' @param baseline_start_sec index number of baseline start
#' @param baseline_stop_sec index number of baseline start
#' @param ... absorbs unused col names for use in pmap
#'
#' @return a numeric vector
#' @export
#'

process <- function(grouped_file, 
                    detrend, 
                    mv2nm, 
                    baseline_start_sec, 
                    baseline_stop_sec,
                    ...){
  
  #Load data and convert mV to nm
  dat <- readr::read_csv(grouped_file) %>%
    dplyr::mutate(nm_converted = bead*mv2nm) %>%
    dplyr::pull(nm_converted)
  
  #PROCESS DATA
  #detrends data by either performing a piecewise linear detrend or simply removing baseline mean from all points (i.e. constant detrend)
  #both of these will center the mean around 0. It just depends if there needs to be long linear drift corrected or not
  
  baseline_center <- if(detrend == "yes"){
    
    break_pts <- seq(25000, length(dat), by = 25000)
    
    as.vector(pracma::detrend(dat, tt = "linear", bp = break_pts))
    
  } else if(detrend == "no"){
    
    get_mean <- mean(dat[ baseline_start_sec : baseline_stop_sec ])
    dat - get_mean
  }
  return(baseline_center)
}
brentscott93/processr documentation built on June 10, 2020, 12:34 a.m.