R/fst_columns.R

Defines functions assert_file_readable fst_nrow fst_columns

Documented in fst_columns fst_nrow

#' Utilities for `fst` files
#' 
#' @param file.fst Path to file.
#' 
#' @return Various outputs:
#' \describe{
#' \item{\code{fst_columns}}{Returns the names of the columns in \code{file.fst}.}
#' \item{\code{fst_nrow}}{Returns the number of rows in \code{file.fst}.}
#' }
#' 
#' @export

fst_columns <- function(file.fst) {
  if (!requireNamespace("fst", quietly = TRUE)) {
    stop("`fst_columns` requires package:fst.") # nocov
  }
  
  assert_file_readable(file.fst, "file.fst")
  fst::metadata_fst(file.fst)[["columnNames"]]
}

#' @rdname fst_columns
#' @export
fst_nrow <- function(file.fst) {
  fst::metadata_fst(file.fst)[["nrOfRows", exact = TRUE]]
}


# nocov start
assert_file_readable <- function(file.ext, vname) {
  if (length(file.ext) != 1) {
    stop("`length(", vname, ") = ", length(file.ext), "` but must be length-one.")
  }
  if (!is.character(file.ext)) {
    stop("`", vname, "` was type '", typeof(file.ext), "' but must be character.")
  }
  if (!file.exists(file.ext)) {
    stop("`", vname, "`:\n\t",
         file.ext, "\n",
         "did not exist.")
  }
  if (file.access(file.ext, mode = 4L)) {
    stop("`", vname, "`:\n\t",
         file.ext, "\n",
         "exists but was not readable.")
  }
}
# nocov end

Try the hutils package in your browser

Any scripts or data that you put into this service are public.

hutils documentation built on April 13, 2022, 5:23 p.m.