From 2f804d2d8c372224ceba6c7b98a50bc45db2e351 Mon Sep 17 00:00:00 2001 From: magrathj Date: Tue, 2 Jul 2019 19:43:22 +0100 Subject: [PATCH 1/4] type: feature Adding feature to enable changing of the flat's filename. Addressing issue @31 "export with a diferent file name #31" --- R/shinyform.R | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/R/shinyform.R b/R/shinyform.R index b530708..ceff92e 100644 --- a/R/shinyform.R +++ b/R/shinyform.R @@ -84,16 +84,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(formInfo$storage$filename)){ + fileName <- paste0( + formInfo$storage$filename, + ".csv" + ) + }else{ + fileName <- paste0( + paste( + format(Sys.time(), "%Y%m%d-%H%M%OS"), + digest::digest(data, algo = "md5"), + sep = "_" + ), + ".csv" + ) + } resultsDir <- storage$path From 2afed06948c2f8a6a9eedc4bf2089cd825a2e7c9 Mon Sep 17 00:00:00 2001 From: magrathj Date: Sun, 4 Aug 2019 12:41:30 +0100 Subject: [PATCH 2/4] Update shinyform.R added changes suggested by daattali in previous pull request --- R/shinyform.R | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/R/shinyform.R b/R/shinyform.R index ceff92e..f7b4bc2 100644 --- a/R/shinyform.R +++ b/R/shinyform.R @@ -79,19 +79,17 @@ 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, filename and storage path saveDataFlatfile <- function(data, storage) { - if(!is.null(formInfo$storage$filename)){ + if (!is.null(storage$filename)) { fileName <- paste0( - formInfo$storage$filename, + storage$filename, ".csv" ) - }else{ + } else { fileName <- paste0( paste( format(Sys.time(), "%Y%m%d-%H%M%OS"), @@ -109,8 +107,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) { From ab420f336effb1befb27852c187f8508e6edc975 Mon Sep 17 00:00:00 2001 From: magrathj Date: Tue, 6 Aug 2019 19:30:17 +0100 Subject: [PATCH 3/4] type: fix Thanks for the feedback, makes sense. Made changes from the review --- R/shinyform.R | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/R/shinyform.R b/R/shinyform.R index f7b4bc2..350a840 100644 --- a/R/shinyform.R +++ b/R/shinyform.R @@ -84,22 +84,18 @@ loadData <- function(storage) { # @param data Dataframe taken from input shiny object # @param storage A list with variable type defining users perferred type of storage, filename and storage path saveDataFlatfile <- function(data, storage) { - if (!is.null(storage$filename)) { - fileName <- paste0( - storage$filename, - ".csv" - ) - } else { - fileName <- paste0( - paste( + + if (is.null(storage$filename) { + fileName <- paste( format(Sys.time(), "%Y%m%d-%H%M%OS"), digest::digest(data, algo = "md5"), sep = "_" - ), - ".csv" ) + } else { + filename <- storage$filename } + filename <- paste0(filename, ".csv") resultsDir <- storage$path # write out the results From f915aaa59b4996c0276b2b6349e206daabe28e60 Mon Sep 17 00:00:00 2001 From: magrathj Date: Fri, 23 Aug 2019 10:41:17 +0100 Subject: [PATCH 4/4] type: test Added test file for testsavedataflatfile function to run unit tests on the function (all passed) and re-ran devtool::test() and devtool::document() --- R/shinyform.R | 8 ++-- tests/testthat/testsaveDataFlatfile.R | 61 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/testsaveDataFlatfile.R diff --git a/R/shinyform.R b/R/shinyform.R index 350a840..000f86e 100644 --- a/R/shinyform.R +++ b/R/shinyform.R @@ -82,20 +82,20 @@ 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, filename and storage path +# @param storage A list with variable type defining users perferred type of storage, fileName and storage path saveDataFlatfile <- function(data, storage) { - if (is.null(storage$filename) { + 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 <- storage$fileName } - filename <- paste0(filename, ".csv") + fileName <- paste0(fileName, ".csv") resultsDir <- storage$path # write out the results 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) + +})