Skip to content

Commit

Permalink
Added back date column, total budget, and total spending over time
Browse files Browse the repository at this point in the history
  • Loading branch information
grasskind committed Aug 29, 2021
1 parent 7f23838 commit 26a27f0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
12 changes: 6 additions & 6 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ server <- function(input, output, session) {
} else if (input$new_or_existing == "Select From Budget") {
category <- input$budget_category
} else {
category <- input$new_category
category <- trimws(input$new_category)
}

# Gather all relevant data
Expand Down Expand Up @@ -148,7 +148,7 @@ server <- function(input, output, session) {
mon.index <- as.numeric(data.table::month(Sys.Date()))
year.index <- as.numeric(data.table::year(Sys.Date()))

# Add category to budget only if the new idtem is from this year/month
# Add category to budget only if the new item is from this year/month
if(new_row$month == mon.index && new_row$year == year.index) {
add_category_to_budget(new_row$category)
}
Expand Down Expand Up @@ -327,7 +327,7 @@ server <- function(input, output, session) {
# Gather data for newly entered category
new_budget_category <- reactive({
# Gather all relevant data
item_data <- data.frame(input$user_add_budget_category,
item_data <- data.frame(trimws(input$user_add_budget_category),
input$user_add_budget_price,
stringsAsFactors = F)
colnames(item_data) <- c("category", "budget")
Expand Down Expand Up @@ -464,7 +464,7 @@ server <- function(input, output, session) {
observe({
updateSelectInput(session, "category", choices = values$session_database$category)
updateSelectInput(session, "budget_category", choices = values$session_budget$category)
updateSelectInput(session, "gp", choices = values$session_database$category)
updateSelectInput(session, "gp", choices = c("Total", values$session_database$category))
updateSelectInput(session, "yr", choices = year(as.Date(values$session_database$date, format = "%m/%d/%y")),
selected = max(as.numeric(year(as.Date(values$session_database$date, format = "%m/%d/%y")))))
})
Expand All @@ -483,8 +483,8 @@ server <- function(input, output, session) {
df$month <- sapply(df$month, function(x) month.name[x], USE.NAMES = F)
DT::datatable(df,
options = list(lengthMenu = c(5, 10, 20, 50, 100),
pageLength = 5,
columnDefs = list(list(visible = F, targets = c(4)))
pageLength = 5
#columnDefs = list(list(visible = F, targets = c(4)))
),
rownames = F,
editable = F)
Expand Down
46 changes: 29 additions & 17 deletions shiny_budget_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ get_date_system_choices <- function() {
# group = spending category, e.g. "Rent"
# year = Year to make plot for
monthly_spending <- function(data, group, yr) {

# Make a new dataframe with the per month spending in group during year
pdata <- data[category == group & year == yr, .(month_total = sum(price)), by = month]
if (group == "Total") {
pdata <- data[year == yr, .(month_total = sum(price)), by = month]
} else {
pdata <- data[category == group & year == yr, .(month_total = sum(price)), by = month]
}

# If no entries match the query, don't plot anything
if(pdata[,.N] == 0) {
Expand All @@ -71,21 +75,29 @@ monthly_spending <- function(data, group, yr) {
pdata$month_abbr <- factor(pdata$month_abbr, levels = month.abb)

# Plot. Title includes average spending over the year
ggplot(pdata, aes(x = month_abbr, y = month_total, group = 1)) +
plt <- ggplot(pdata, aes(x = month_abbr, y = month_total, group = 1)) +
geom_line(size = 1.5) +
geom_point(size = 2, color = "purple") +
expand_limits(y = 0) +
ggtitle(paste0("Monthly Spending on ", group,", Average: $", round(mean(pdata$month_total)))) +
labs(x = "Month", y = "Total Spent") +
theme(text=element_text(face="bold"),
axis.text=element_text(size=12),
axis.title=element_text(size=14),
title=element_text(size=16),
axis.ticks.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank()
)
expand_limits(y = 0)

if (group == "Total") {
plt <- plt + ggtitle(paste0("Total Monthly Spending", ", Average: $", round(mean(pdata$month_total))))
} else {
plt <- plt + ggtitle(paste0("Monthly Spending on ", group,", Average: $", round(mean(pdata$month_total))))
}


plt <- plt +
labs(x = "Month", y = "Total Spent") +
theme(text=element_text(face="bold"),
axis.text=element_text(size=12),
axis.title=element_text(size=14),
title=element_text(size=16),
axis.ticks.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
plt
}


Expand Down Expand Up @@ -212,7 +224,7 @@ monthly_budget_remaining <- function(data, budget_df, color_df) {
geom_col() +
geom_point(mapping = aes(x = category, y = budget)) +
scale_fill_manual(values = plot_df$col) +
ggtitle(paste0("Spending in ", month.name[mon.index]," of ", year.index, ", Total: ", sum(plot_df$month_total))) +
ggtitle(paste0("Spending in ", month.name[mon.index]," of ", year.index, ", Spent: ", sum(plot_df$month_total), ", Budget: ", sum(plot_df$budget))) +
xlab("Category") +
ylab("Spending ($)") +
theme(text=element_text(face="bold"),
Expand All @@ -225,4 +237,4 @@ monthly_budget_remaining <- function(data, budget_df, color_df) {
panel.grid.minor = element_blank(),
panel.background = element_blank()) +
geom_text(aes(label=round(remaining)), position=position_dodge(width=0.9), vjust=-0.25)
}
}
6 changes: 1 addition & 5 deletions ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ library(shiny)
library(shinyhelper)
library(magrittr)
library(shinydashboard)
library(dashboardthemes)


source("shiny_budget_helpers.R")
Expand All @@ -11,7 +10,7 @@ DATE_SYSTEM_CHOICES <- get_date_system_choices()

# UI for Shiny Budget App
ui <- dashboardPage(

skin = "blue",
dashboardHeader(title = "Shiny Budget"),

dashboardSidebar(
Expand All @@ -24,9 +23,6 @@ ui <- dashboardPage(

),
dashboardBody(
shinyDashboardThemes(
theme = "poor_mans_flatly"
),
tabItems(
tabItem(tabName = "dashboard",
fluidRow(
Expand Down

0 comments on commit 26a27f0

Please sign in to comment.