convertCsvFile: Modify the format of a CSV file

View source: R/csv.R

convertCsvFileR Documentation

Modify the format of a CSV file

Description

This function allows you to change the format (such as column delimiter, decimal character) of a CSV file. It uses read.table to read a CSV file and write.table to rewrite the file with modified format to a new file. All arguments of read.table and write.table are supported. Arguments that are provided by both functions appear as two arguments <argument_name>_in and <argument_name>_out in this function.

Usage

convertCsvFile(
  file_in,
  sep_in = formals(utils::read.table)$sep,
  sep_out = sep_in,
  dec_in = formals(utils::read.table)$dec,
  dec_out = dec_in,
  file_out = NULL,
  header = TRUE,
  quote_in = formals(utils::read.table)$quote,
  quote_out = formals(utils::write.table)$quote,
  row.names_in = formals(utils::read.table)$row.names,
  col.names_in = formals(utils::read.table)$col.names,
  row.names_out = FALSE,
  col.names_out = TRUE,
  fileEncoding_in = formals(utils::read.table)$fileEncoding,
  fileEncoding_out = fileEncoding_in,
  dbg = TRUE,
  ...
)

Arguments

file_in

path to input file

sep_in

column separator in input file

sep_out

column separator in output file

dec_in

decimal character in input file

dec_out

decimal character inoutput file

file_out

path to output file

header

passed to read.table

quote_in

passed as quote to read.table

quote_out

passed as quote to write.table

row.names_in

passed as row.names to read.table

col.names_in

passed as col.names to read.table

row.names_out

passed as row.names to write.table

col.names_out

passed as col.names to write.table

fileEncoding_in

passed as fileEncoding to read.table

fileEncoding_out

passed as fileEncoding to write.table

dbg

if TRUE (default) debug messages are shown

...

further arguments passed to either read.table or write.table

Value

path to the created CSV file

Examples

# Write the iris dataset to a temporary file with "," as column separator
csv_in <- tempfile(fileext = ".csv")
write.table(iris, csv_in, row.names = FALSE)

# Review the first lines of the file
catLines(readLines(csv_in, 6))

# Convert the column separator (from " " which was the default) to ";"
csv_out <- convertCsvFile(csv_in, sep_out = ";")

# Review the result
catLines(readLines(csv_out, 6))

# Delete the file so that it can be recreated
unlink(csv_out)

# Convert the column separator and the decimal character
csv_out <- convertCsvFile(csv_in, sep_out = ";", dec_out = ",")

# Review the result
catLines(readLines(csv_out, 6))


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.