knitr::opts_chunk$set(
  error = TRUE,
  collapse = TRUE,
  comment = "#>",
  out.width = "100%"
)
library(testthat)
library(jeksterslabRutils)
context("Test util_fn2columns.")

Parameters

Initialize temporary folder in the working directory

tmp <- util_make_subdir()

Copy files from extdata to temporary folder

pattern <- "^filename.*"
extdata <- system.file(
  "extdata",
  "tests",
  package = "jeksterslabRutils",
  mustWork = TRUE
)
files_from <- list.files(
  path = extdata,
  pattern = pattern
)
full_path_files_from <- file.path(
  extdata,
  files_from
)
full_path_files_to <- file.path(
  tmp,
  files_from
)
file.copy(
  from = full_path_files_from,
  to = full_path_files_to
)
root <- basename(tmp)
fn_output <- file.path(
  tmp,
  paste0(
    root,
    ".csv"
  )
)
fn_column <- TRUE
save <- FALSE
rows <- 1108
columns <- 10
if (fn_column) {
  columns <- columns + 1 # columns plus fn_column
}
Variable <- c(
  "`tmp`",
  "`pattern`",
  "`fn_column`",
  "`save`",
  "`rows`",
  "`columns`"
)
Description <- c(
  "Working directory.",
  "Pattern.",
  "Save file name of source data file as a new column.",
  "Save concatenated files in `csv` format.",
  "Number of rows.",
  "Number of columns."
)
Value <- c(
  tmp,
  pattern,
  fn_column,
  save,
  rows,
  columns
)
knitr::kable(
  x = data.frame(
    Variable,
    Description,
    Value
  ),
  row.names = FALSE
)

Run test

csv <- util_bind(
  dir = tmp,
  format = "csv",
  pattern = "^filename.*",
  fn_column = TRUE,
  save = FALSE,
  save_dir = tmp,
  par = FALSE
)

util_fn2columns

df <- as.data.frame(
  csv
)
df <- util_fn2columns(
  df = df,
  colnames = c("root", "number", "ext")
)
head(df)
test_that("expect_true", {
  expect_true(
    ("root" %in% colnames(df)),
    ("number" %in% colnames(df)),
    ("ext" %in% colnames(df))
  )
})

Expect error

test_that("error", {
  expect_error(
    util_fn2columns(
      df = "NOT A DATA FRAME"
    )
  )
})
test_that("message", {
  expect_message(
    util_fn2columns(
      df = as.data.frame(
        csv
      ),
      colnames = paste0("col", 1:20)
    )
  )
})

No colnames

util_fn2columns(
  df = as.data.frame(
    csv
  )
)

Clean up temporary files and folders

util_clean_dir(
  dir = tmp,
  create_dir = FALSE
)


jeksterslabds/jeksterslabRutils documentation built on Jan. 18, 2021, 11:41 p.m.