-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpos.R
194 lines (154 loc) · 6.16 KB
/
pos.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
library(shiny)
library(shinydashboard)
library(DT)
library(shinydashboardPlus)
library(ggplot2)
library(dplyr)
library(lubridate)
# Define UI for app
ui <- dashboardPage(
# Define app header
dashboardHeader(title = "Point of Sale App"),
# Define app sidebar
dashboardSidebar(
sidebarMenu(
menuItem("Transactions", tabName = "transactions", icon = icon("credit-card")),
menuItem("Sales Data", tabName = "sales_data", icon = icon("chart-bar")),
menuItem("Sales Charts", tabName = "sales_charts", icon = icon("chart-pie"))
)
),
# Define app body
dashboardBody(
# Define tab items
tabItems(
# Define "Transactions" tab
tabItem(tabName = "transactions",
# Define input widgets
fluidRow(
box(title = "Item" ,selectInput("vendor", "Vendor",
choices = c("Vendor A", "Vendor B", "Vendor C")),
selectInput("category","Category",
choices = c("Electronics", "Furniture", "Toys")),
textInput("item", "Item Description"),
numericInput("price", "Price", value = NULL),
actionButton("add_item", "Add Item"),
actionButton("remove_item", "Remove Item")),
infoBoxOutput("total"),
actionButton("checkout", "Checkout"),
box(dataTableOutput("receipt_table"))
# Define receipt table
)
),
# Define "Sales Data" tab
tabItem(tabName = "sales_data",
# Define input widgets
fluidRow(
column(3, selectInput("filter_vendor", "Vendor",
choices = c("All", "Vendor A", "Vendor B", "Vendor C"))),
column(3, dateRangeInput("filter_date", "Date Range")),
column(3, actionButton("filter", "Filter"))
),
# Define sales data table
br(),
dataTableOutput("sales_table")
),
#define the graph tab page
tabItem(tabName = "sales_charts",
fluidRow(
box(width = 12, height = 500,
plotOutput("sales_chart"))
)
)
)
)
)
# Define server for app
server <- function(input, output, session) {
# Define reactive receipt data frame
receipt_data <- reactiveValues(items = data.frame(item = character(),
price = numeric(),
stringsAsFactors = FALSE))
# Add an item to the receipt
observeEvent(input$add_item, {
new_item <- data.frame(item = input$item, price = input$price)
receipt_data$items <- rbind(receipt_data$items, new_item)
updateTextInput(session, "item", value = "")
updateNumericInput(session, "price", value = 0)
})
# Remove an item from the receipt
observeEvent(input$remove_item, {
if (nrow(receipt_data$items) > 0) {
receipt_data$items <- receipt_data$items[-nrow(receipt_data$items), ]
}
})
# Define receipt table output
output$receipt_table <- renderDataTable({
receipt_data$items
}, options = list(searching = FALSE, lengthChange = FALSE))
# Define total widget output
output$total <- renderInfoBox({
total <- sum(receipt_data$items$price) * 1.07
infoBox(
"Total",
paste0("$", total),
icon = icon("dollar-sign"),
color = "teal",
tags$style("font-size: 36px;")
#paste0("$", total), "Total", color = "teal", icon = icon("dollar-sign")
)
})
observeEvent(input$checkout, {
# Create sales.csv file if it doesn't exist
if (!file.exists("sales.csv")) {
write.csv(data.frame(date = character(), vendor = character(),
catagory = character(),
item = character(), price = numeric(),
stringsAsFactors = FALSE),
"sales.csv", row.names = FALSE)
}
# Append transaction details to sales.csv
datetime <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
transaction_data <- cbind(datetime, vendor = input$vendor, category = input$category,
receipt_data$items)
write.table(transaction_data, "sales.csv", sep = ",",
col.names = FALSE, row.names = FALSE,
append = TRUE)
# Clear receipt
receipt_data$items <- data.frame(item = character(), price = numeric(),
stringsAsFactors = FALSE)
})
sales_data_subset <- reactive({
# Read in sales.csv
sales_data <- read.csv("sales.csv", stringsAsFactors = FALSE)
sales <- reactive({
sales <- read.csv("sales.csv", stringsAsFactors = FALSE)
sales$date <- lubridate::ymd_hms(sales$date)
sales
})
output$sales_chart <- renderPlot({
sales_data <- sales()
sales_data <- sales_data %>% filter(year(date) == year(Sys.Date()), month(date) == month(Sys.Date()))
sales_data <- sales_data %>% group_by(vendor) %>% summarize(month_sales = sum(price))
ggplot(sales_data, aes(x=vendor, y=month_sales, fill=vendor)) +
geom_col() +
ggtitle("Monthly Sales by Vendor") +
xlab("Vendor") +
ylab("Month-to-Date Sales")
})
# Filter sales data based on input widgets
if (input$filter_vendor != "All") {
sales_data_subset <- sales_data[sales_data$vendor == input$filter_vendor, ]
} else {
sales_data_subset <- sales_data
}
if (!is.null(input$filter_date)) {
sales_data_subset <- sales_data_subset[sales_data_subset$date >= input$filter_date[1] &
sales_data_subset$date <= input$filter_date[2], ]
}
sales_data_subset
})
output$sales_table <- renderDataTable({
sales_data_subset()
})
}
shinyApp(ui, server)