Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Map and popup #25

Merged
merged 6 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
processed.R
rsconnect/

.Rhistory
93 changes: 78 additions & 15 deletions app.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
library(shiny)
library(ggplot2)
library(dplyr)
library(plotly)
library(leaflet)
library(geosphere)
library(leaflet.extras)
library(sf)


# Load dataset
data <- read.csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-12-20/weather_forecasts.csv")
weather <- read.csv("data/processed/weather_pro.csv")
cities <- read.csv("data/processed/cities.csv")

# Define UI
ui <- fluidPage(
Expand All @@ -19,44 +26,100 @@ ui <- fluidPage(
sliderInput("month_range", "Select Month Range:",
min = 1, max = 12, value = c(1, 12)),


# Add dropdown menu input for selecting state
selectInput("state", "Select State:",
choices = unique(data$state)),
choices = unique(weather$state)),

# Add dropdown menu input for selecting city
selectInput("city", "Select City:",
choices = unique(data$city)),
choices = NULL),

# Add radio button input for selecting temperature or precipitation
radioButtons("data_type", "Select Data Type:",
choices = c("Temperature", "Precipitation"),
selected = "Temperature")
selected = "Temperature"),
),

# Add main panel with plot output
mainPanel(
plotOutput("line_plot")
plotOutput("line_plot"),
leafletOutput("map")
)
)
)

# Define server
server <- function(input, output) {
server <- function(input, output, session) {

# Update city and state input based on map clicks
observeEvent(input$map_marker_click, {
updateSelectInput(session, "city", selected = input$map_marker_click$id)
updateSelectInput(session, "state", selected = cities$state[cities$city == input$map_marker_click$id])
})

# Update city input possible values based on selected state
observe({
updateSelectInput(session, "city",
choices = unique(weather$city[weather$state == input$state]))

})
# observe({
# updateSelectInput(session, "state",
# choices = unique(weather$state[weather$city == input$city]))
# })



# Filter data based on user inputs
filtered_data <- reactive({
data %>%
filter(month >= input$month_range[1], month <= input$month_range[2]) %>%
filter(state == input$state, city == input$city)
line_data <- reactive({
weather %>%
filter(month >= input$month_range[1], month <= input$month_range[length(input$month_range)]) %>%
filter(state == input$state, city == input$city) %>%
group_by(month, high_or_low) %>%
summarise(observed_temp = mean(observed_temp, na.rm=TRUE),
observed_precip = mean(observed_precip, na.rm=TRUE))
})

# Create line plot based on filtered data and user data type input
output$line_plot <- renderPlot({
ggplot(filtered_data(), aes(x = month, y = ifelse(input$data_type == "Temperature", observed_temp, observed_precip))) +
geom_line() +
labs(x = "Month", y = input$data_type)
if (input$data_type == "Temperature"){
ggplot(line_data(), aes(x = month, y = observed_temp, col=high_or_low)) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = seq(1, 12, by = 1)) +
labs(x = "Month", y = "Temperature (°F)", color="High/Low")
}
else{
ggplot(line_data(), aes(x = month, y = observed_precip)) +
geom_point(color="violetred") +
geom_line(color="lightblue") +
scale_x_continuous(breaks = seq(1, 12, by = 1)) +
labs(x = "Month", y = "Precipitation")
}
})

# Create leaflet map
output$map <- renderLeaflet({
leaflet(cities) |>
addTiles() |>
addCircleMarkers(~lon,
~lat,
popup = paste0("City: ", cities$city, "<br>",
"State: ", cities$state, "<br>",
"Elevation: ", cities$elevation, " m<br>",
"Distance to Coast: ", cities$distance_to_coast, " mi<br>",
"Average Annual Precipitation: ", cities$avg_annual_precip, " in"),
layerId = cities$city,
label = cities$city,
color = "navy",
radius = 5,
stroke = FALSE,
fillOpacity = 0.4) |>
setView(-100, 40, zoom = 3.3)
})

}

# Run app
shinyApp(ui, server)
shinyApp(ui, server)
Loading