This script will demonstrate how to:
Start by creating folders to export the data to:
for (folder in c("csv", "tab", "tex", "tsv", "txt", "xls", "xlsx")) {
dir.create(file.path(folder), showWarnings = FALSE)
}
st <- "Lorem ipsum dolor sit amet"
vct <- c("Hello", "World")
df <- data.frame(
Name = c("Charles Babbage", "Ada Lovelace", "Alan Turing", "Mary Somerville", "John Conway", "Katherine Johnson"),
Age = c(79, 36, 41, 91, 82, 101),
DOB = c("1791-12-26", "1815-12-10", "1912-06-23", "1780-12-26", "1937-12-26", "1918-08-26")
)
Export string to a text file (.txt):
cat("Hello", file = "txt/string.txt", sep = "\n")
cat("World", file = "txt/string.txt", append = TRUE, sep = "\n")
Export vector to a text file (.txt):
fileConn <- file("txt/vector.txt")
writeLines(vct, fileConn)
close(fileConn)
Export a data frame as comma-separated values (.csv):
# Option 1
utils::write.csv(df, "csv/data frame.csv")
# Option 2
readr::write_csv(df, "csv/data frame.csv")
# Include the row names (this is only an option with write.csv() from utils)
write.csv(df, "csv/data frame.csv", row.names = TRUE)
Export a data frame as tab-separated values (.tsv):
write.table(df, file = "tsv/data frame.tsv", sep = "\t", row.names = FALSE)
Export a data frame as tab-separated values (.tab):
write.table(df, file = "tab/data frame.tab", sep = "\t", row.names = FALSE)
Export a data frame as tab-separated values to a text file (.txt):
write.table(df, file = "txt/data frame.txt", sep = "\t", row.names = FALSE)
Export a data frame to an Excel 97-2004 workbook (.xls):
library(WriteXLS)
WriteXLS(df, "xls/data frame.xls")
Export a data frame to an Excel 2007 workbook (.xlsx):
library(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "Sheet 1")
writeData(wb, "Sheet 1", df)
saveWorkbook(wb, "xlsx/data frame.xlsx", overwrite = TRUE)
Export a data frame as a kable table to a Latex file (.tex):
library(kableExtra)
kable_input <- kable(df, "latex", booktabs = TRUE, escape = FALSE)
kable_output <- kable_styling(kable_input, latex_options = c("striped"))
cat(kable_output, file = "tex/kable table.tex", append = FALSE)
The “rjson” library is useful for working with JavaScript Object Notation (JSON) data:
library(rjson)
Export a data frame as a JSON object to a JSON file (.json):
json <- rjson::toJSON(df)
file <- file("json/json object.json")
writeLines(json, file)
close(file)
Export a data frame as a JSON object to a text file (.txt):
json <- rjson::toJSON(df)
file <- file("txt/json object.txt")
writeLines(json, file)
close(file)
Redirect console output to text file:
sink("txt/diary.txt")
print("Hello world")
sink()
Import comma-separated values (.csv) as a data frame:
df <- utils::read.csv("csv/data frame.csv")
Readr’s read_csv()
will produces console output if you don’t specify the column types. Either manually specify these or use col_types = cols()
to have R guess the types:
library(readr)
df <- readr::read_csv("csv/data frame.csv", col_types = cols())
If there are blank rows at the top of the CSV, skip them when importing it:
df <- read.csv("csv/data frame.csv", skip = 2)
Import from the internet:
data_url <- "https://raw.githubusercontent.com/rowannicholls/rowannicholls.github.io/master/gapminder.csv"
gapminder <- read.csv(url(data_url))
Import all CSVs in a folder as a list of data frames:
filenames <- list.files(path = "csv", pattern = "*.csv")
filepaths <- file.path("csv", filenames)
ls <- lapply(filepaths, read.csv)
Use the file names to create a named list which can then be indexed with either a number or the file names themselves:
names(ls) <- filenames
# These two will now both work to extract the same data frame
df <- ls[["data frame.csv"]]
df <- ls[[1]]
Import tab-separated values (.tsv) as a data frame:
df <- read.table("tsv/data frame.tsv", sep = "\t", header = TRUE)
df <- read.delim("tsv/data frame.tsv", header = TRUE)
Import tab-separated values (.tab) as a data frame:
df <- read.table("tab/data frame.tab", sep = "\t", header = TRUE)
df <- read.delim("tab/data frame.tab", header = TRUE)
Import tab-separated values from a text file (.txt) as a data frame:
df <- read.table("txt/data frame.txt", header = TRUE)
Import text file (.txt) as a string:
st <- readLines("txt/string.txt")
print(st)
## [1] "Hello" "World"
Import text file (.txt) as a data frame:
df <- read.table("txt/data frame.txt", header = TRUE)
The “rjson” library is useful for working with JavaScript Object Notation (JSON) data:
library(rjson)
Import a single JSON file:
file_location <- "json/json object.json"
json_data <- rjson::fromJSON(file = file_location)
Import a single JSON file that has been saved as a txt:
file_location <- "txt/json object.txt"
json_data <- rjson::fromJSON(paste(readLines(file_location), collapse=""))
Import all JSON files in a folder:
dir <- "json_files"
# List all JSON files in a folder and its sub-folders
files <- list.files(dir, pattern = "*.json", recursive = TRUE)
# Construct the relative paths to the JSON files
filepaths <- file.path(dir, files)
# Initialise list of JSON data
all_json_data <- vector(mode = "list")
# Import all the files
for (filepath in filepaths) {
# Import files
json_data <- rjson::fromJSON(file = filepath)
# Append to list
all_json_data[[length(all_json_data) + 1]] <- json_data
}
Import JSON data from a web address:
json_file <- "http://api.worldbank.org/country?per_page=10®ion=OED&lendingtype=LNX&format=json"
json_data <- rjson::fromJSON(file = json_file)
Import an Excel 97-2004 workbook (.xls) as a tibble:
library(readxl)
tib <- readxl::read_excel("xls/data frame.xls")
Then convert it to a data frame:
df <- as.data.frame(tib)
Import an Excel 2007 workbook (.xlsx) as a tibble:
library(readxl)
tib <- readxl::read_excel("xlsx/data frame.xlsx")
Then convert it to a data frame:
df <- as.data.frame(tib)
To import a specific worksheet, use the sheet
keyword argument and either the sheet’s name or index:
tib <- readxl::read_excel("xlsx/data frame.xlsx", sheet = "Sheet 1")
tib <- readxl::read_excel("xlsx/data frame.xlsx", sheet = 1)
Use skip
to only import the rows below a certain point:
tib <- readxl::read_excel("xlsx/data frame.xlsx", skip = 3)