-
Notifications
You must be signed in to change notification settings - Fork 0
/
aex_app.R
73 lines (56 loc) · 1.91 KB
/
aex_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
library(shiny)
library(jsonlite)
# Define UI
ui <- fluidPage(
titlePanel("AEX Stock Data"),
sidebarLayout(
sidebarPanel(
selectInput("company", "Select Company", choices = c("ADYEN.AS", "ASML.AS", "UNA.AS", "HEIA.AS", "INGA.AS", "RDSA.AS", "PHIA.AS", "DSM.AS", "ABN.AS", "KPN.AS")),
dateRangeInput("dates", "Select Date Range", start = Sys.Date() - 365, end = Sys.Date())
),
mainPanel(
plotOutput("stock_plot")
)
)
)
# Define server
server <- function(input, output) {
# Plot stock data
output$stock_plot <- renderPlot({
ticker <- input$company
dates <- input$dates
url <- paste0(
"https://query1.finance.yahoo.com/v8/finance/chart/",
ticker,
"?period1=",
as.numeric(as.POSIXct(dates[1])),
"&period2=",
as.numeric(as.POSIXct(dates[2])),
"&interval=1d"
)
# Generate a unique callback function name
callback <- paste0("callback", as.integer(Sys.time()))
# Append the callback function to the URL as a query parameter
jsonp_url <- paste0(url, "&format=json&callback=", callback)
print(jsonp_url)
# Download data
download.file(jsonp_url, "stockdata.json")
print(list.files())
# Parse JSON data
parsed_data <- fromJSON("stockdata.json")
# Extract the necessary data from the parsed JSON
prices <- parsed_data$chart$result$indicators$quote[[1]]$close[[1]]
dates <- as.Date(as.POSIXct(parsed_data$chart$result$timestamp[[1]], origin = "1970-01-01"))
stock_data <- data.frame(Date = dates, Close = prices, stringsAsFactors = FALSE)
print(stock_data)
plot(x = stock_data$Date,
y = stock_data$Close,
type = "l",
col = "steelblue",
xlab = "Date",
ylab = "Closing Price",
main = paste("Stock Data for", ticker))
})
}
# Run the application
shinyApp(ui = ui, server = server)