library(tidyverse)
path <- getwd()
isRunningInShiny <- Sys.getenv('SHINY_PORT') != ""
if(endsWith(path,'R') | isRunningInShiny){
source('Schemas/MintSchemas.R')
source('Mock/mockMint.R')
} else {
source('R/Schemas/MintSchemas.R')
source('R/Mock/mockMint.R')
}
setClass("Mint", slots =c(transactions = "tbl_df",
curDate = "Date"),
prototype=list(
transactions = NULL
))
#Creator
Mint = function(curDate) {
initDs <- getMintInit()
clnDs <- transformMint(initDs)
new("Mint",transactions=clnDs,curDate = curDate)
}
#' Get Init Mint dataset
#' @return data frame
#' @export
#' @examples
#' getMintInit()
getMintInit <- function() {
getGoogleSheetFromSheetName("Ravi - mint")
#mockMintInit()
}
#' Mock for a Transaction sheet
#' @return mocked Transaction Sheet
#' @export
#' @examples
#' mockTranInit()
mockMintInit <- function() {
m1 = createMint(BilledDate = "11/26/2017",
Establishment = "Wegman's",
Amount = "-$42")
m2 = createMint(BilledDate = "11/28/2017",
Establishment = "Trader Joe's",
Amount = "-$81",
Reconciled = "Yes")
initMint <- dplyr::union_all(m1,m2)
}
#' Transform the transaction dataset
#' @param x dataset from getGoogleSheet
#' @return data frame
#' @export
#' @examples
#' init <- getGoogleSheet("Ravi - mint")
#' transformMint(init)
transformMint <- function(ds,weeks="ALL") {
if(weeks == "ALL")
weekData <- ds
else{
numWeeks <- as.numeric(weeks)
weekData <- filter( ds,difftime(curDate,
as.Date(BilledDate,"%m/%d/%Y"),
units="weeks") <= numWeeks )
}
#Clean the input dataset
clnDs <- mutate(weekData,
clnReceiptNo = as.character(ReceiptNo),
clnBilledDate=as.Date(BilledDate,"%m/%d/%Y"),
clnTransactionDate=as.Date(TransactionDate,"%m/%d/%Y"),
clnAmount = clnDollar(Amount))
#transformation based on clean dataset
trDs <- mutate(clnDs,
trTransactionDateMonthYear =format(clnTransactionDate, "%d-%b"),
trnBilledDateMonthYear =format(clnBilledDate, "%d-%b"),
trBilledDateStr=format(clnBilledDate, "%m/%d/%Y")
)
#arrange( desc(tBilledDate))
#Remove unnecessary columns to bring to target Dataset
select(trDs,-ReceiptNo,-BilledDate,-TransactionDate,-Amount,-Filler1,-Filler2,-Filler3)
}
#' Perform Data Quality Validations
#' @param x dataset from transformMint
#' @return data frame
#' @export
#' @examples
#' init <- getGoogleSheet("Ravi - mint")
#' tr <- transformMint(init)
#' validateMint(tr)
setMethod(f="validate", signature="Mint", definition = function(object,showErrors) {
ds <- object@transactions
#Make sure our schema is cln schema
checkSchema(ds,clnMintSchema)
#Clean the input dataset
vlds <- mutate(ds,
clnFail = if_else(is.na(clnAmount),'Amount column is empty or malformed',NULL) )
#Add error indicator
dq <- mutate(vlds,
failed = if_else(is.na(clnFail),FALSE,TRUE) )
#Arrange records
finDs <- arrange(dq,desc(clnBilledDate))
#Show only error output if corresponding option is chose
if(showErrors)
filter(finDs,failed)
else
finDs
})
#' Subset data based on weeks required
#' @param numWeeks No of weeks to subset the data on
#' @return data frame
#' @export
setMethod(f="subsetTransactions", signature=c("Mint","character"), definition = function(object,chrWeeks) {
ds <- object@transactions
if(chrWeeks == "ALL")
weekData <- ds
else{
numWeeks <- as.numeric(chrWeeks)
weekData <- filter( ds,difftime(object@curDate,
as.Date(clnBilledDate,"%m/%d/%Y"),
units="weeks") <= numWeeks )
}
weekData
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.