-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
127 lines (108 loc) · 3.19 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
124
125
126
readRenviron(".Renviron")
KEY <- Sys.getenv("FINNHUB_KEY")
library(shiny)
library(shinyalert)
library(jsonlite)
library(httr)
# Define the UI
ui <- fluidPage(
titlePanel(
HTML("IPO Calendar <br> <small>Past/upcoming IPOs around selected date</small>"),
windowTitle = "IPO Calendar"
),
tags$style(
HTML("
.date-box {
width: 180px;
height: 180px;
border: 1px solid #ccc;
display: inline-block;
margin: 5px;
text-align: center;
vertical-align: top;
font-size: 14px;
line-height: 1.5em;
background-color: #FFEEDE;
}
.highlighted {
background-color: #FFDDC1;
font-weight: bold;
border: 3px solid #FFBB99;
}
.sidebar {
width: 15%;
}
")
),
fluidRow(
column(
2,
class = "sidebar",
dateInput("date", "Select a date:", format = "yyyy-mm-dd")
),
column(
10,
uiOutput("calendar")
)
)
)
server <- function(input, output) {
fetch_ipo_data <- function(from, to) {
base_url <- "https://finnhub.io/api/v1/calendar/ipo"
api_key <- KEY
request_url <- sprintf("%s?from=%s&to=%s&token=%s", base_url, from, to, api_key)
response <- GET(request_url)
data <- fromJSON(content(response, "text"))
return(data$ipoCalendar)
}
output$calendar <- renderUI({
selected_date <- input$date
from <- as.Date(selected_date) - 25
to <- as.Date(selected_date) + 5
ipo_data <- fetch_ipo_data(from, to)
date_range <- seq(from, to, by = "1 day")
ui_elements <- lapply(date_range, function(date_current) {
date_text <- div(as.character(date_current))
ipos_on_date <- subset(ipo_data, date == as.character(date_current))
ticker_links <- if (nrow(ipos_on_date) > 0) {
lapply(1:nrow(ipos_on_date), function(row_num) {
ipo <- ipos_on_date[row_num,]
# alert_text <- sprintf(
# "Symbol: %s, Name: %s, Price: %s, Date: %s, Exchange: %s, Shares: %s, Total Value: %s",
# ipo$symbol, ipo$name, ipo$price, ipo$date,
# ipo$exchange, ipo$numberOfShares, ipo$totalSharesValue
# )
alert_text <- sprintf(
"Symbol: %s\\nName: %s\\nPrice: %s\\nDate: %s\\nExchange: %s\\nShares: %s\\nTotal Value: %s",
ipo$symbol, ipo$name, ipo$price, ipo$date, ipo$exchange, ipo$numberOfShares, ipo$totalSharesValue
)
return(tags$a(
href = "#",
onclick = sprintf("Shiny.setInputValue('show_alert', {alert_text: '%s'}, {priority: 'event'}); return false;", alert_text),
ipo$symbol,
br() # The br() ensures that each ticker is on a new line.
))
})
} else {
NULL
}
return(
div(
class = ifelse(date_current == as.Date(selected_date), "date-box highlighted", "date-box"),
date_text,
ticker_links
)
)
})
return(ui_elements)
})
observeEvent(input$show_alert, {
shinyalert::shinyalert(
title = "IPO Details",
text = input$show_alert$alert_text,
type = "info"
)
})
}
# Run the app
shinyApp(ui, server)