-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpense tracker.R
76 lines (68 loc) · 2.15 KB
/
expense tracker.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Expense Tracker UI
expense_tracker_ui <- function(id) {
ns <- NS(id)
tagList(
sidebarLayout(
sidebarPanel(
textInput(ns("item"), "Enter Item Name:", ""),
numericInput(ns("amount"), "Enter Amount:", value = 0, min = 0),
actionButton(ns("add"), "Add Expense"),
actionButton(ns("clear"), "Clear All Expenses"),
hr(),
tableOutput(ns("expense_table"))
),
mainPanel(
plotOutput(ns("expense_plot")),
downloadButton(ns("download"), "Download Expenses")
)
)
)
}
# Expense Tracker Server
expense_tracker_server <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
# Reactive value to store expenses
expenses <- reactiveVal(data.frame(Item = character(), Amount = numeric(), stringsAsFactors = FALSE))
# Add new expense
observeEvent(input$add, {
if (input$item == "" || input$amount <= 0) {
showNotification("Please provide a valid item and amount.", type = "error")
return()
}
new_expense <- data.frame(Item = input$item, Amount = input$amount, stringsAsFactors = FALSE)
expenses(rbind(expenses(), new_expense))
})
# Clear all expenses
observeEvent(input$clear, {
expenses(data.frame(Item = character(), Amount = numeric(), stringsAsFactors = FALSE))
})
# Render expense table
output$expense_table <- renderTable({
expenses()
})
# Render expense plot
output$expense_plot <- renderPlot({
expense_data <- expenses()
if (nrow(expense_data) > 0) {
barplot(
height = expense_data$Amount,
names.arg = expense_data$Item,
col = "blue",
main = "Expenses",
xlab = "Items",
ylab = "Amount"
)
}
})
# Download expenses as CSV
output$download <- downloadHandler(
filename = function() {
paste("expenses-", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(expenses(), file, row.names = FALSE)
}
)
})
}