write_clean_csv() writes a data frame to a CSV file using
readr::write_csv() and emits a cli confirmation message reporting
the number of rows and columns written. It is the natural counterpart
to read_clean_csv(), reinforcing the convention that data-raw/
holds original inputs and data/ holds cleaned, analysis-ready outputs.
Arguments
- data
A data frame or tibble to write.
- path
A character string with the path to the output CSV file.
- overwrite
Logical. If
FALSE(the default), errors if the file already exists. Set toTRUEto overwrite an existing file.- ...
Additional arguments passed to
readr::write_csv(), such asappend,col_names, orquote.
Details
If column names are not already clean, write_clean_csv() applies
janitor::clean_names() before writing and emits a warning listing
the affected columns.
Examples
# \donttest{
sample_path <- system.file("templates", "sample.csv", package = "toolero")
data <- read_clean_csv(sample_path)
# Write to a temp file
out <- tempfile(fileext = ".csv")
write_clean_csv(data, out)
#> ✔ Wrote 344 rows and 8 columns to /tmp/RtmpCGYXJ9/file4de54950458a.csv.
# Overwrite an existing file
write_clean_csv(data, out, overwrite = TRUE)
#> ✔ Wrote 344 rows and 8 columns to /tmp/RtmpCGYXJ9/file4de54950458a.csv.
# Dirty names are cleaned automatically with a warning
dirty <- data.frame("First Name" = "Jane", "Last Name" = "Doe",
check.names = FALSE)
write_clean_csv(dirty, tempfile(fileext = ".csv"))
#> Warning: Column names were not clean -- applying `janitor::clean_names()` before
#> writing. Affected columns: "First Name" and "Last Name"
#> ✔ Wrote 1 row and 2 columns to /tmp/RtmpCGYXJ9/file4de55573e509.csv.
# }
