R/write_csv_threadr.R

Defines functions write_csv_threadr

Documented in write_csv_threadr

#' Function to export data frame/tibble to a \code{.csv} text file with certain 
#' defaults. 
#' 
#' @author Stuart K. Grange
#' 
#' @param df A data frame or tibble to write to disk.
#' 
#' @param file File to write \code{df} to. 
#' 
#' @param format_dates Should the dates in \code{df} be converted to a 
#' \code{\%Y-\%m-\%d \%H:\%M:\%S} string? 
#' 
#' @param na String used for missing values. 
#' 
#' @return Invisible \code{df}.
#' 
#' @export
write_csv_threadr <- function(df, file, format_dates = TRUE, na = "") {
  
  # Check object
  stopifnot(inherits(df, "data.frame"))
  
  # Format dates
  if (format_dates) {
    df <- mutate(
      df, 
      across(
        tidyselect::vars_select_helpers$where(lubridate::is.POSIXt), 
        format,
        format = "%Y-%m-%d %H:%M:%S"
      )
    )
  }
  
  # Format other things
  df <- mutate(
    df, 
    across(tidyselect::vars_select_helpers$where(is.logical), as.integer)
  )
  
  # Export
  readr::write_csv(df, file = file, na = na)
  
  return(invisible(df))
  
}
skgrange/threadr documentation built on May 11, 2024, 12:16 p.m.