I'm writing a 3D stock portfolio model with R using Shiny
The ui.R is:
# UI
# Libraries
library(shiny)
# Interface
shinyUI(fluidPage(
titlePanel("3D Stock Projection"),
sidebarLayout(
sidebarPanel(
helpText("Select a stock to examine.
Information will be collected from yahoo finance."),
textInput("symb", "Symbol", "SPY"),
dateRangeInput("dates",
"Date range",
start = "2013-01-01",
end = as.character(Sys.Date())),
actionButton("get", "Get Stock"),
br(),
br(),
checkboxInput("log", "Plot y axis on log scale",
value = FALSE),
checkboxInput("adjust", "Adjust prices for inflation",
value = FALSE)
),
mainPanel(plotOutput("plot"))
)
))
the server.R is:
# Server
# Libraries
library(shiny)
library(TTR)
library(scales)
library(quantmod)
library(scatterplot3d)
#Equations
shinyServer(function(input, output) {
#This finds holding period
time <- reactive({
getSymbols(input$symb,
src="yahoo",
from = input$dates[1],
to = input$dates[2],
auto.assign = FALSE)
})
#Basic yield equaition in percentage
yield <- reactive({
initial <- as.number(getQuote(input$symb,
src="yahoo",
from = input$date[1],
auto.assign = FALSE)
)
final <- as.number(getQuote(input$symb,
src="yahoo",
from = input$date[2],
auto.assign = FALSE)
)
as.number(percent(((final-initial)/initial)))
})
#This finds the alpha by retrieving the S&P yield
alpha <- reactive({
marketInitial <- as.number(getQuote("S&P500",
src="yahoo",
from = input$date[1],
auto.assign = FALSE)
)
marketFinal <- as.number(getQuote("S&P500",
src="yahoo",
from = input$date[2],
auto.assign = FALSE)
)
as.number(percent(((marketFinal-marketInitial)/marketInitial)))
})
#3D Projection
output$plot <- renderPlot({
scatterplot3d(time, yield, alpha,
highlight.3d = TRUE,
col.axis = "blue",
col.grid = "ligthblue",
main = "Helix",
pch = 20)
})
})
When I run it the interface opens up successfully but the plot just doesn't appear, and instead it gives me an error saying that I cannot cannot "coerce type 'closure' to vector of type 'double'"
I think the problem is the date system and the way I am calculating time, which is difficult because I have to manage dates as opposed to numbers and none of the other instances of this problem on Stackoverflow address the closure issue with something as complicated as a date.
I wanna thank everyone for your help in advance XD