-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrencyApp.R
158 lines (108 loc) · 3.92 KB
/
currencyApp.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
library(shiny)
library(shinythemes)
library(ggplot2)
library(plotly)
source('getData.R')
ui <- fluidPage(theme = shinytheme("united"),
titlePanel(h1("verinak's Currency Converter"), windowTitle = "verinak's Currency Converter"),
div("Data collected from ", a('Fraknfurter', href='https://www.frankfurter.app/', target='_blank')),
hr(),
br(),
fluidRow(
# inputs
column(6,
br(),
# from
fluidRow(
column(4,
numericInput("amountFrom", label = NULL, value = 1),),
column(6, align = 'left',
selectInput("currFrom", label = NULL,
choices = NULL,
selected = NULL),),
),
# to
fluidRow(
column(4,
verbatimTextOutput("amountTo", placeholder = TRUE),),
column(6, align = 'left',
selectInput("currTo", label = NULL,
choices = NULL,
selected = NULL),),
),
# date
fluidRow(
column(1, style='margin-top: 10px;',
strong(span("At: ")),),
column(4,
dateInput("date", label = NULL, value = Sys.Date(), min='1999-01-04'),),
column(2,
actionButton("todayDate", "Today?"),),
),
),
# plot
column(6, align = 'center',
radioButtons("plotPeriod", label=NULL, choices=c('1W'=7, '1M'=30, '1Y'=365), inline=TRUE),
plotlyOutput('plot'),
),
),
)
server <- function(input, output, session){
# get currencies on load
currencies <<- getCurrencies()
updateSelectInput(session, 'currFrom', choices=currencies, selected = currencies[1])
updateSelectInput(session, 'currTo', choices=currencies, selected = currencies[2])
# convert from currFrom to currTo
observeEvent(list(input$currFrom,input$currTo,input$amountFrom,input$date), {
if(input$currFrom == "" || input$currTo == "" || is.na(input$amountFrom) || length(input$date) == 0){
return()
}
currFrom <<- input$currFrom
currTo <<- input$currTo
amount <- input$amountFrom
date <- input$date
if(input$date == Sys.Date()){
conversion <- getLatestConversion(currFrom, currTo, amount)
}
else {
conversion <- getHistoricalConversion(currFrom, currTo, amount, date)
}
output$amountTo <- renderText({conversion})
dateOld = as.Date(date) - as.numeric(input$plotPeriod)
output$plot <- renderPlotly({
getPlot(dateOld, date, currFrom, currTo)
})
})
observeEvent(input$plotPeriod, {
if(input$currFrom == "" || input$currTo == "" || length(input$date) == 0){
return()
}
date <- input$date
dateOld <- as.Date(date) - as.numeric(input$plotPeriod)
output$plot <- renderPlotly({
getPlot(dateOld, date, currFrom, currTo)
})
})
# switch currFrom and currTo if same currency is selected
observeEvent(input$currFrom, {
if(input$currFrom=="") {
return()
}
if(input$currFrom == input$currTo){
updateSelectInput(session, 'currTo', selected = currFrom)
}
})
observeEvent(input$currTo, {
if(input$currTo=="") {
return()
}
if(input$currFrom == input$currTo){
updateSelectInput(session, 'currFrom', selected = currTo)
}
})
# today button sets date to today
observeEvent(input$todayDate, {
updateDateInput(session, "date", value=Sys.Date())
})
}
shinyApp(ui = ui, server = server)