Learn R Programming

rio (version 0.2)

convert: Convert from one file format to another

Description

This function constructs a data frame from a data file using code{import} and uses export to write the data to disk in the format indicated by the file extension.

Usage

convert(in_file, out_file, in_opts = list(), out_opts = list())

Arguments

in_file
a character string naming an input file.
out_file
a character string naming an output file.
in_opts
a named list of options to be passed to import
out_opts
a named list of options to be passed to export

Value

  • The name of the output file (invisibly).

Examples

Run this code
# create a file to convert
export(iris, "iris.dta")

# convert Stata to CSV and open converted file
convert("iris.dta", "iris.csv")
head(import("iris.csv"))

# correct an erroneous file format
export(iris, "iris.csv", format = "tsv")
convert("iris.csv", "iris.csv", in_opts = list(format = "tsv"))

# convert serialized R data.frame to JSON
export(iris, "iris.rds")
convert("iris.rds", "iris.json")

# cleanup
unlink("iris.csv")
unlink("iris.dta")
unlink("iris.rds")
unlink("iris.json")

# convert from the command line:
Rscript -e "rio::convert('iris.dta', 'iris.csv')"

Run the code above in your browser using DataLab