-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
172 lines (161 loc) · 5.21 KB
/
app.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
# Load library
library(shiny)
library(data.table)
library(DT)
# read and row bind all data sets
listings <-
rbindlist(lapply("redfin_forsale.csv", fread),
use.names = TRUE)
recentlysold <-
rbindlist(lapply("redfin_recentlysold.csv", fread),
use.names = TRUE)
# UI client
ui <-
fluidPage(titlePanel("House Hunt"),
mainPanel(tabsetPanel(
id = "mainPanel",
tabPanel("Listings",
mainPanel(
"Click on any row",
fluidRow(DT::dataTableOutput("listings_table"))
)),
tabPanel(
"Comparables",
mainPanel(
"Property Selected:",
htmlOutput("comparableList"),
DT::dataTableOutput("recentlysold_table")
),
sidebarPanel("Comparable Stats",
htmlOutput("statsList"))
),
tabPanel(
"About",
"The data here is downloaded from Redfin.com as a CSV using search for 'Alameda County' in RedFin.com.
The recently sold data is also downloaded from Redfin.com."
)
)))
# Server
server <- function(input, output, session) {
output$listings_table = DT::renderDataTable(DT::datatable({
listings
}
, options = list(pageLength = 50) , selection = 'single'),
server = TRUE)
# await any user input and apply filters
observeEvent(input$listings_table_rows_selected, {
address <- listings[input$listings_table_rows_selected]$ADDRESS
city <- listings[input$listings_table_rows_selected]$CITY
zip <- listings[input$listings_table_rows_selected]$ZIP
beds <- listings[input$listings_table_rows_selected]$BEDS
baths <- listings[input$listings_table_rows_selected]$BATHS
location <-
listings[input$listings_table_rows_selected]$LOCATION
hoa <- listings[input$listings_table_rows_selected]$`HOA/MONTH`
sqft <-
listings[input$listings_table_rows_selected]$`SQUARE FEET`
yearbuilt <-
listings[input$listings_table_rows_selected]$`YEAR BUILT`
mlslisting <-
listings[input$listings_table_rows_selected]$`MLS#`
redfinurl <-
listings[input$listings_table_rows_selected]$`URL (SEE http://www.redfin.com/buy-a-home/comparative-market-analysis FOR INFO ON PRICING)`
listingprice <-
listings[input$listings_table_rows_selected]$PRICE
recentlysold <-
recentlysold[toupper(recentlysold$CITY) == toupper(city) &
recentlysold$BEDS == beds &
recentlysold$BATHS >= baths &
recentlysold$`SQUARE FEET` >= (sqft - 300) &
recentlysold$`SQUARE FEET` <= (sqft + 300) &
recentlysold$`YEAR BUILT` >= (yearbuilt - 5) &
recentlysold$`YEAR BUILT` <= (yearbuilt + 5)]
output$recentlysold_table = DT::renderDataTable(DT::datatable({
recentlysold
}
, options = list(pageLength = 50) , selection = 'single'),
server = TRUE)
# render output
output$comparableList <- renderText({
paste(
"<br><b> <a target='_blank' href='",
redfinurl,
"'>Redfin URL</a> </b>",
"<br>",
"<b> <a target='_blank' href='https://www.realtor.com/realestateandhomes-search?mlslid=",
mlslisting,
"'>Realtor URL</a> </b>",
"<br>",
"<b>Listing Price:</b> ",
listingprice,
"<br>",
"<b>Address:</b> ",
address,
",",
city,
"CA ",
zip,
"<br>",
"<b>Beds:</b> ",
beds,
"<br>",
"<b>Baths:</b> ",
baths,
"<br>",
"<b>Sqft:</b> ",
sqft,
"<br>",
"<b>HOA / Month:</b> ",
hoa,
"<br>",
"<b>Year Built:</b> ",
yearbuilt,
"<br>",
"<b>Location:</b> ",
location,
"<br><br>"
)
})
# recalcuate pricing
avgListingPrice <-
round(mean(recentlysold$PRICE, na.rm = TRUE), 2)
avgDaysOnMarket <-
round(mean(recentlysold$`DAYS ON MARKET`, na.rm = TRUE), 2)
avgSqft <-
round(mean(recentlysold$`SQUARE FEET`, na.rm = TRUE), 2)
avgHOA <- round(mean(recentlysold$`HOA/MONTH`, na.rm = TRUE), 2)
avgYearBuilt <-
round(mean(recentlysold$`YEAR BUILT`, na.rm = TRUE), 0)
earliestSold <- max(recentlysold$`SOLD DATE`)
latestSold <- min(recentlysold$`SOLD DATE`)
# display pricing
output$statsList <- renderText({
paste(
"<br><b>Avg Sale Price:</b> ",
avgListingPrice,
"<br>",
"<b>Avg Days on Market:</b> ",
avgDaysOnMarket,
"<br>",
"<b>Avg. Sqft:</b> ",
avgSqft,
"<br>",
"<b>Avg HOA / Month:</b> ",
avgHOA,
"<br>",
"<b>Avg Year Built:</b> ",
avgYearBuilt,
"<br>",
"<b>Earliest Sold:</b> ",
earliestSold,
"<br>",
"<b>Latest Sold:</b> ",
latestSold
)
})
# update tabs
updateTabsetPanel(session, "mainPanel", selected = "Comparables")
})
}
# init app
shinyApp(ui, server)