我正在使用Shiny
编写一个3D股票投资组合模型ui.R是:
# 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"))
)
))
server.R是:
# 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)
})
})
当我运行它时,界面打开成功,但情节不会出现,而是它给我一个错误,说我不能&#34; 强制类型&#39;关闭&# 39;到&#39; double&#39; &#34;
的矢量我认为问题是日期系统和我计算时间的方式,这很困难,因为我必须管理日期而不是数字,而且Stackoverflow上的这个问题的其他任何实例都没有解决关闭问题复杂的约会。
我想提前感谢大家的帮助XD