-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoral_bleach_app.R
90 lines (74 loc) · 2.86 KB
/
coral_bleach_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
# importing the required libraries
library(shiny)
library(leaflet)
library(ggplot2)
# reading the dataset into a data frame
coral_data <- read.csv("assignment-02-data-formated.csv")
# re-formatting the variable 'value' to numeric data type
coral_data$value <- gsub("%", "", coral_data$value)
coral_data$value <- as.numeric(coral_data$value)/100
# ordering site locations by increasing value of latitudes (lower to upper)
lat_order <- unique(coral_data[c("location", "latitude")])
lat_order <- lat_order[order(lat_order$latitude),]
coral_data$location <- factor(coral_data$location, levels = lat_order$location)
# shiny application
# server script for the web application
server <- function(input, output) {
# genrating the reactive tabular plot
output$coralPlot <- renderPlot({
coral = input$type
# extracting a section of the data frame belonging to the selected coral type
if (coral == 'all')
df <- coral_data
else
df <- coral_data[coral_data$coralType == coral,]
p <- ggplot(df, aes(x=year, y=value)) +
geom_point() +
facet_grid(coralType~location) +
geom_smooth(method = input$model)
p <- p + labs(y = "rate")
print(p)
})
# generating a map through leaflet
output$mymap <- renderLeaflet({
leaflet(data = coral_data) %>%
addTiles() %>%
addMarkers(~longitude, ~latitude, popup = ~as.character(location),
label=~as.character(location),
labelOptions = labelOptions(noHide = T, textOnly = TRUE,
direction = "left", textsize = "10px"))
})
}
# defining the UI application
ui <- fluidPage(
# application title
titlePanel("Coral Bleaching"),
# drop-down menu for selcting type of coral and smoother
mainPanel(
fluidRow(
column(5, selectInput("type", "Coral Type:",
list("All" = "all",
"Blue Corals" = "blue corals",
"Hard Corals" = "hard corals",
"Sea Fans" = "sea fans",
"Sea Pens" = "sea pens",
"Soft Corals" = "soft corals"), width='200px')),
column(5, selectInput("model", "Smoothers:",
list("Auto" = "auto",
"Linear Model" = "lm",
"Generalised Linear Model" = "glm",
"Generalised Additive Model" = "gam",
"Loess Model" = "loess"), width='200px'))
),
# showing the reactive tabluar plot
plotOutput("coralPlot")
),
# creating a sidebar to display the map
sidebarPanel(
br(), br(),
# creating a canvas map on the application
leafletOutput("mymap")
)
)
# rendering the web application
shinyApp(ui, server)