Skip to content

Commit

Permalink
content: map time
Browse files Browse the repository at this point in the history
  • Loading branch information
hlydecker committed Jul 2, 2023
1 parent 0bf7f21 commit 8556518
Showing 1 changed file with 61 additions and 5 deletions.
66 changes: 61 additions & 5 deletions rentscraper/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ library(ggplot2)
library(data.table)
library(lubridate)
library(zoo)
library(leaflet)
library(rvest)
library(stats)
library(tidygeocoder)
library(tidyverse)
library(GGally)
library(ggmap)
library(plotly)

library(shiny)
Expand Down Expand Up @@ -97,7 +100,7 @@ ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h2("Location"),
selectInput("state", "State:", choices = c("QLD","NSW","VIC","SA","TAS","WA","ACT")),
selectInput("state", "State:", choices = c("NSW","VIC","QLD","SA","TAS","WA","ACT")),
textInput("postcode", "Postcode:", "2088"),
textInput("suburb", "Suburb:", "Mosman"),
h2("Unit Details"),
Expand All @@ -112,8 +115,14 @@ ui <- fluidPage(
),
mainPanel(
tabsetPanel(
tabPanel("Visualisations", plotlyOutput("boxplots")),
tabPanel("Data", DT::dataTableOutput("results"))
tabPanel("Visualisations",
plotlyOutput("boxplots"),
plotlyOutput("histogram")
),
tabPanel("Data", DT::dataTableOutput("results")),
tabPanel("Map",
h4("NOTE: this feature is still under development and will take some time to load."),
leafletOutput("map"))
)
)
)
Expand All @@ -139,7 +148,7 @@ server <- function(input, output) {

output$boxplots <- renderPlotly({

p <- scraped_data() %>%
scraped_data() %>%
drop_na() %>%
mutate(month = reorder(
factor(format(month, '%b %Y')), as.numeric(interaction(month(month), year(month)))
Expand All @@ -158,8 +167,55 @@ server <- function(input, output) {
title = paste0('Distribution of weekly rent in ',input$suburb, ", ", input$state),
subtitle = paste0(input$beds, " beds", input$baths, " baths")
)
})

# Generate the histogram
output$histogram <- renderPlotly({
scraped_data() %>%
drop_na() %>%
filter(month >= Sys.Date() - months(3)) %>%
ggplot(aes(x = price)) +
geom_histogram(binwidth = 50, fill = "lightblue", color = "black") +
geom_vline(xintercept = input$current_rent, color = "dark green") +
geom_vline(xintercept = input$new_rent, color = "red") +
theme_bw() +
theme(panel.grid = element_blank()) +
labs(
x = 'Weekly rent',
y = 'Count',
title = 'Distribution of Weekly Rents in the Last 3 Months'
)
})

ggplotly(p)
# Generate the map
output$map <- renderLeaflet({
# Create a leaflet map object
map <- leaflet() %>%
addTiles() # Add the default map tiles

# Geocode addresses and add markers to the map
data <- scraped_data()
if (!is.null(data) && nrow(data) > 0) {
geocoded_data <- tidygeocoder::geocode(data, address = "address", method = "osm")

# Add markers with rent and configuration information
for (i in seq_along(geocoded_data$lat)) {
address <- data$address[i]
rent <- data$price[i]
config <- data$config[i]

map <- map %>%
addMarkers(
lat = geocoded_data$lat[i],
lng = geocoded_data$long[i],
popup = paste("Address:", address, "<br>",
"Rent:", rent, "<br>",
"Configuration:", config)
)
}
}

map # Return the modified map object
})

}
Expand Down

0 comments on commit 8556518

Please sign in to comment.