This is a follow-up question from This Question which was correctly answered by @K.Rohde. I am unable to call an input from UI as an argument in a user-defined function. Below is the complete reproducible shiny code:
library(shiny)
library(lubridate)
library(dplyr)
rm(list = ls())
Aging <- function(data, Transaction.Date, Report.Date = Sys.Date()){
if(missing(Transaction.Date)) stop("You forgot to specify Transaction Date")
Transaction.Date <- deparse(substitute(Transaction.Date))
Report.Date <- deparse(substitute(Report.Date))
data$year <- year(data[,Transaction.Date])
data$Age <- car::recode(year(data[,Transaction.Date]), paste0("year(", Report.Date, ") = year(", Report.Date, "); year(", Report.Date, ") - 1 = year(", Report.Date, ") - 1; else = paste(year(", Report.Date, ")-2, 'And Prior')"))
return(data)
}
ui <- fluidPage(
dateInput("Report.Date", "Enter Cut-off Date"),
actionButton("Submit", "Submit"),
dataTableOutput("Aged.Data")
)
server <- function(input, output) {
Debtors <- eventReactive(input$Submit, {
data.frame(Names = c("John", "Mary", "Charles", "Peter", "David", "Fabian", "Aggrey", "Elizabeth", "Anthony", "Catherine"), Amount = seq(from = 100000, by = 600, length.out = 10), Transaction.Date = seq.Date(from = as.Date("2016/1/1"), by = "quarter", length.out = 10))
})
Aged.Data <- eventReactive(input$Submit, {
Debtors() %>% Aging(., Transaction.Date, input$Report.Date)
})
output$Aged.Data <- renderDataTable(Aged.Data())
}
shinyApp(ui, server)
The error I get is as below:
in recode term: else = paste(year(input$Report.Date)-2, 'And Prior')
message: Error in year(input$Report.Date) : object 'input' not found
This seems to be from the part where I'm calling my Aging function and trying to reference the input$Report.Date as an argument, i.e,
Aged.Data <- eventReactive(input$Submit, {
Debtors() %>% Aging(., Transaction.Date, input$Report.Date)
I've tried looking online for a similar solution through questions like This one and This one and though the error is similar, the proposed methods of resolving them seem to be unique to those questions.
I will appreciate any assistance on this.
Joseph.
Agingfunction without specifying theReport.Dateparameter, the app works just fine, meaning that theAgingfunction is in order.in recode term: else = paste(year(reportDate)-2, 'And Prior') message: Error in year(reportDate) : object 'reportDate' not foundReport.Dateargument which will affect the output for other users.