R/read_feem.R

Defines functions read_feem

Documented in read_feem

#' Read FEEM data files generated by the Horiba Aqualog
#'
#' @param path Path to stored FEEM data files
#' @param truncate Set negative intensities to zero?
#'
#' @return A tibble with columns `emission`, `em_regular`, `excitation`, and `intensity`.
#' `em_regular` is a regularly-spaced version of `emission` that is useful for plotting.
#' @importFrom dplyr mutate if_else across n_distinct arrange rename %>%
#' @importFrom tidyr pivot_longer
#' @importFrom tidyselect everything
#' @importFrom readr read_csv
#' @importFrom rlang .data
#' @export
#'
#' @examples
#' file <- list.files(
#'    path = system.file("extdata", package = "cwrshelpr"),
#'    full.names = TRUE,
#'    pattern = ".+\\.csv"
#' )
#' read_feem(file[1])
read_feem <- function(path, truncate = FALSE) {
  `Sample - Blank` <- NULL
  emission <- NULL
  em_regular <- NULL
  out <- read_csv(file = path)[-(1:2), ] %>%
    mutate(across(everything(), as.numeric)) %>%
    rename(emission = `Sample - Blank`) %>%
    # create a regular sequence of emission wavelengths:
    arrange(.data$emission) %>%
    mutate(
      em_regular = seq(
        min(.data$emission),
        max(.data$emission),
        length.out = n_distinct(.data$emission)
      )
    ) %>%
    pivot_longer(
      -c(emission, em_regular),
      names_to = "excitation",
      values_to = "intensity"
    ) %>%
    mutate(excitation = as.numeric(.data$excitation))

  if(truncate) {
    out %>%
      mutate(across(everything(), ~ if_else(.x < 0, 0, .x)))
  } else out
}
bentrueman/cwrshelpr documentation built on July 1, 2023, 4:29 a.m.