## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(awkreader)

## -----------------------------------------------------------------------------
data.path <- system.file("extdata", "ratings_data", package = "awkreader")

all.files <- list.files(path = data.path, full.names = TRUE)

the.files <- all.files[1:2]

combined.fread(the.files = the.files)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, nrows = 5)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, nrows = 5, file.header = "source_file")

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, nrows = 5, include.filename = F)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, the.variables = c("item", "rating"), nrows = 5)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, the.variables = c("item", "rating"), nrows = 5, return.data.table = F)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, nrows = 5, drop = c(1,3), include.filename = F)

combined.fread(the.files = the.files, nrows = 5, drop = c("user", "rating"), include.filename = F)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, the.variables = c("item", "rating"), return.as = "code")

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, the.variables = c("item", "rating"), nrows = 5, return.as = "all")

## -----------------------------------------------------------------------------
# Count records in all CSV files in the data directory
combined.fread(the.files = data.path, file.pattern = "*.csv")

## -----------------------------------------------------------------------------
combined.fread(the.files = data.path, file.pattern = "csv", recursive = TRUE)

## -----------------------------------------------------------------------------
combined.fread(the.files = the.files, return.as = "code")

## -----------------------------------------------------------------------------
combined.fread(the.files = data.path, skip = 0)
# combined.fread(the.files = data.path, skip = "pattern_to_match")

## -----------------------------------------------------------------------------
combined.fread(the.files = data.path, skip = list(skip.data.rows = 4, skip.metadata.rows = 0))

## -----------------------------------------------------------------------------
combined.fread(the.files = data.path, delim = ",")

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = NULL, nrows = 5, include.filename = F)

## -----------------------------------------------------------------------------
## Write filtering language in R's syntax
filtered.fread(the.files = the.files, the.filter = "rating == 5", nrows = 5)

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = "rating >= 3 & item == '1fg4sLgEFzAtOqCa'")

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = 'rating >= 3 & item == "1fg4sLgEFzAtOqCa"')

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = 'rating == 3 | rating == 4', nrows = 5)

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = 'rating != 1 & rating != 2 & rating != 3 & rating != 4', nrows = 5)

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = 'rating >= 3 & item %in% c("1fg4sLgEFzAtOqCa", "6qI9cBWT76jxm42G")')

filtered.fread(the.files = the.files, the.filter = 'rating >= 3 & item %in% c("1fg4sLgEFzAtOqCa", "6qI9cBWT76jxm42G")', return.as = "code")

## -----------------------------------------------------------------------------
two.items <- c("1fg4sLgEFzAtOqCa", "6qI9cBWT76jxm42G")
filtered.fread(the.files = the.files, the.filter = 'rating >= 3 & item %in% two.items', return.as = "all")

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, the.filter = 'rating %nin% c(1:2, 4)', nrows = 5)

## -----------------------------------------------------------------------------
the.output <- filtered.fread(the.files = all.files, the.filter = 'rating >= 4 & item %in% two.items', include.filename = T, num.files.per.batch = 10, show.warnings = FALSE, return.as = "all", nrows = 5)

print(the.output$result)
print(the.output$code[1:2])

## -----------------------------------------------------------------------------
filtered.fread(the.files = the.files, header = FALSE, the.filter = "V3==5", include.filename = F, drop = c("V1", "V2"), return.as = "all")

## ----dfd----------------------------------------------------------------------
pattern.fread(the.files = the.files, the.patterns = "5n9ziP", return.as = "all")

## -----------------------------------------------------------------------------
pattern.fread(the.files = the.files, the.patterns = "5n9ziP", tf = FALSE, return.as = "all")

## -----------------------------------------------------------------------------
pattern.fread(the.files = the.files, the.patterns = c("QPW5X7c", "ziPoS"), connectors = "and", return.as = "result")

## -----------------------------------------------------------------------------
pattern.fread(the.files = the.files, the.patterns = c("ThHYoPWn4IVJ", "ziPoS", "jTXm3t"), connectors = c("or", "or"))

## -----------------------------------------------------------------------------
pattern.fread(the.files = the.files, the.patterns = c("W5X7", "ziPoS", "jTXm3t"), connectors = c("and", "or"), return.as = "all")

## -----------------------------------------------------------------------------
titanic.file <- system.file("extdata", "titanic.csv", package = "awkreader")
pattern.fread(the.files = titanic.file, the.patterns = c("Female", "Child", "1st"), tf = c(T, T, F), connectors = c("or", "and"))

## -----------------------------------------------------------------------------
record.count(the.files = the.files)

## -----------------------------------------------------------------------------
record.count(the.files = the.files, the.filter = "rating == 5")

## -----------------------------------------------------------------------------
aggregated.fread(
  the.files = the.files,
  group.by = "item",
  summarize.with = list(mean = "rating", median = "rating")
)

## -----------------------------------------------------------------------------
aggregated.fread(
 the.files = the.files,
 summarize.with = list(mean = list("rating"), sd = list("rating"), median = list("rating")),
 group.by = c("user", "item")
)

## -----------------------------------------------------------------------------
aggregated.fread(
  the.files = the.files,
  group.by = "item",
  summarize.with = list(mean = "rating", sd = "rating", sample.size = TRUE)
)

## -----------------------------------------------------------------------------
aggregated.fread(
  the.files = the.files,
  group.by = "item",
  summarize.with = list(mean = "sqrt(rating)", sd = "log(rating)")
)

## -----------------------------------------------------------------------------
aggregated.fread(
  the.files = the.files,
  group.by = "user",
  summarize.with = list(median = "rating"),
  sample.size.median = -1
)

## -----------------------------------------------------------------------------
aggregated.fread(
  the.files = the.files,
  group.by = "user",
  summarize.with = list(cor = "sqrt(item), log(item)")
)

