diff --git a/R/shinyform.R b/R/shinyform.R index b530708..000f86e 100644 --- a/R/shinyform.R +++ b/R/shinyform.R @@ -79,22 +79,23 @@ loadData <- function(storage) { } } - - # Takes data from your shinyforms inputs and saves it to a flat file. # Writes form inputs to a storage type and names it using a timestamp. # @param data Dataframe taken from input shiny object -# @param storage A list with variable type defining users perferred type of storage and storage path +# @param storage A list with variable type defining users perferred type of storage, fileName and storage path saveDataFlatfile <- function(data, storage) { - fileName <- paste0( - paste( - format(Sys.time(), "%Y%m%d-%H%M%OS"), - digest::digest(data, algo = "md5"), - sep = "_" - ), - ".csv" - ) + if (is.null(storage$fileName)) { + fileName <- paste( + format(Sys.time(), "%Y%m%d-%H%M%OS"), + digest::digest(data, algo = "md5"), + sep = "_" + ) + } else { + fileName <- storage$fileName + } + + fileName <- paste0(fileName, ".csv") resultsDir <- storage$path # write out the results @@ -102,8 +103,6 @@ saveDataFlatfile <- function(data, storage) { row.names = FALSE, quote = TRUE) } - - # Takes data from a flat file and passes it to your shiny app. # @param storage A list with variable type defining users perferred type of storage loadDataFlatfile <- function(storage) { diff --git a/tests/testthat/testsaveDataFlatfile.R b/tests/testthat/testsaveDataFlatfile.R new file mode 100644 index 0000000..0c194fa --- /dev/null +++ b/tests/testthat/testsaveDataFlatfile.R @@ -0,0 +1,61 @@ + + +library(shiny) + +context("Testing saveDataFlatfile Func") + + +test_that("check saveDataFlatfile outputs fileName when fileName is specified", { + + # create data input + data <- data.frame(name = "", age = 0, favourite_pkg = "", terms = TRUE) + path = getwd() + + # create storage input + storage = list( + type = STORAGE_TYPES$FLATFILE, + fileName = "TestingExample", + path = path + ) + + # use saveDataFlatfile function + saveDataFlatfile(data, storage) + fileName <- file.path(path, "TestingExample.csv") + + # test what we expect against the resulting csv + expect_equal(fileName, paste0(path, '/', dir(path=path, pattern="*csv")[1])) + + ## Clean up + files <- paste0(path, '/', dir(path=path, pattern="*csv*")) + file.remove(files) + +}) + + + + + +test_that("check saveDataFlatfile outputs fileName when fileName is not specified", { + + # create data input + data <- data.frame(name = "", age = 0, favourite_pkg = "", terms = TRUE) + path = getwd() + + # create storage input + storage = list( + type = STORAGE_TYPES$FLATFILE, + path = path + ) + + # use saveDataFlatfile function + saveDataFlatfile(data, storage) + + + # test what we expect against the resulting csv + expect_equal(52, nchar(dir(path=path, pattern="*csv")[1])) + + ## Clean up + files <- paste0(path, '/', dir(path=path, pattern="*csv*")) + file.remove(files) + +})