Introduction
R can export data to various formats for sharing and further analysis.
Exporting to CSV
# Base R
write.csv(df, "data.csv", row.names = FALSE)
# Readr
library(readr)
write_csv(df, "data.csv")
write_tsv(df, "data.tsv")
Exporting to Excel
library(openxlsx)
# Write to Excel
write.xlsx(df, "data.xlsx")
# Multiple sheets
write.xlsx(list(df1 = df1, df2 = df2), "data.xlsx")
Exporting to Other Formats
# RDS
saveRDS(df, "data.rds")
# Text file
write.table(df, "data.txt", sep = "\t")
# JSON
library(jsonlite)
toJSON(df, pretty = TRUE)
# Database
library(DBI)
dbWriteTable(con, "table_name", df)
Exporting Plots
# PNG
png("plot.png")
plot(x, y)
dev.off()
# PDF
pdf("plot.pdf")
plot(x, y)
dev.off()
# SVG
svg("plot.svg")
plot(x, y)
dev.off()
Summary
Export data in the format needed for your downstream use or sharing requirements.