R/extraction.R

Defines functions extract_vector extract_code

Documented in extract_code extract_vector

# Source file: extraction.R
#
# MIT License
#
# Copyright (c) 2019 Victor Ordu

#' Extracting code from REDCap Script  File
#'
#'
#' @name extract
NULL

#' @rdname extract
#'
#' @param rgx A reguar expression
#' @param txt A portion of text, supplied as a character vector
#'
#' @import stringr
#'
#' @return A character vector with the extracted code
#'
#' @export
extract_code <- function(rgx, txt) {
  stopifnot(is.character(rgx))
  stopifnot(is.character(txt))

  txt %>%
    str_subset(rgx) %>%
    str_replace(rgx, "\\2")
}



#' @rdname extract
#'
#' @param str A character string containing the code
#'
#' @return A vector from the evaluated code
#'
#' @note This is an internal function but is exported to enable use in
#' the main RAAMP_GBV project.
#'
#' @export
extract_vector <- function(str) {
  stopifnot(exprs =
              {
                is.character(str)
                isFALSE(anyNA(str))
              })
  str <- eval(parse(text = str))
  gsub("\\s?", "", str)
}
BroVic/jGBV documentation built on Oct. 9, 2022, 6:38 a.m.