Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions R/shinyform.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,30 @@ 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
write.csv(x = data, file = file.path(resultsDir, fileName),
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) {
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/testsaveDataFlatfile.R
Original file line number Diff line number Diff line change
@@ -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)

})