knitr::opts_chunk$set( error = TRUE, collapse = TRUE, comment = "#>", out.width = "100%" )
library(testthat) library(jeksterslabRutils) context("Test util_fn2columns.")
tmp <- util_make_subdir()
extdata
to temporary folderpattern <- "^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 )
csv <- util_bind( dir = tmp, format = "csv", pattern = "^filename.*", fn_column = TRUE, save = FALSE, save_dir = tmp, par = FALSE )
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)) ) })
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) ) ) })
util_fn2columns( df = as.data.frame( csv ) )
util_clean_dir( dir = tmp, create_dir = FALSE )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.