-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
123 lines (97 loc) · 3.1 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
# Loading the shiny app.
#' @title Function \code{app}
#' @description Runs the Shiny app.
#' @import shiny
#' @import plotly
#' @import ggplot2
#' @export app
library(plotly)
# Function for getting
# the last month of a specific date.
get_month = function(date){
# Date should be a string
# of the format: "y%-m%-d%".
# Getting the month.
month = as.numeric(substr(date, 6, 7))
# If it's January, the month needs to be changed
# to December and a year needs to be substracted.
if (month == 1){
# From January to December.
month = 12
# Going to the last year.
year = as.numeric(substr(date, 1, 4))
year = as.character(year - 1)
substr(date, 1, 4) = year
} else {
# Otherwise substract the a month.
month = month - 1
}
# Getting the month as a character
month = as.character(month)
# Verifying it's well formated and if
# not, correct it.
if (nchar(month) != 2){
month = paste("0", month, sep="")
}
substr(date, 6, 7) = month
# Return modified date.
return(date)
}
# Starting the data processing.
# Setting up the token.
Sys.setenv("NOAA_TOKEN" = "RCxwJXwZHAFJRIzCPdfLhOshjnRycvio")
# Getting the cities data.
cities = get_cities()
# Needs to be a function so nested dependencies are lazily loaded.
# My ui.
ui = function(){fluidPage(
# Title of the shiny app.
titlePanel("CDO Visualization App"),
sidebarLayout(position="left",
sidebarPanel(
selectInput(inputId="country",
label="Select Country Code",
choices=unique(cities$country)),
uiOutput("drop_down"),
p("Get the country codes",
a("here", href="https://docs.opendata.aws/noaa-ghcn-pds/readme.html#lookup-table-of-country-codes"))),
mainPanel(plotlyOutput("weather_plot")))
)}
# My server.
server = function(input, output){
output$drop_down = renderUI(
if (is.null(input$country)){
return()
}
else{
selectInput(inputId="city",
label="Select a city",
choices=cities$city[cities$country == input$country])
}
)
# Filtering to get the city info.
output$weather_plot = renderPlotly(
if (is.null(input$city)){
p = NULL
return(p)
} else {
city_info = cities[cities$city == input$city, ]
weatherdata = get_weatherdata("TAVG",
city_info$id,
get_month(city_info$maxdate),
city_info$maxdate,
check=FALSE)
weatherdata_simplified = simplify_weatherdata(weatherdata)
p = plot_ly(type="scatter", mode="lines") %>%
add_trace(x=weatherdata_simplified$date,
y=weatherdata_simplified$TAVG,
mode="lines+markers",
name=paste("Last month average temperature of:", input$city))
return(p)
}
)
}
my_app = function(){
shinyApp(ui=ui(), server=server)
}
my_app()