我已经编写了一个用于计算R中股票相关性的代码,这就是代码。
library(quantmod)
tickers<-c('AAPL','GOOG','MSFT')
stockData=new.env()
getSymbols(tickers,src = "yahoo", env=stockData,from =as.Date("2016-01-01"))
library(PerformanceAnalytics)
x <- list()
Data=data.frame()
clo=data.frame()
for (i in 1:length(tickers)) {
x[[i]] <- get(tickers[i], pos=stockData) # get data from stockData environment
clo<-cbind(clo,Cl(x[[i]]))
Data=cbind(Data,diff(log(Cl(x[[i]]))))
}
chart.Correlation(Data)
Cl(Data)
tail(clo)
但是我很难将其转换为闪亮的应用程序
到目前为止,我已经在我的闪亮应用程序中完成了。我选择shinysky
库来选择多个股票
ui.R
#ui.R
library(shinysky)
shinyUI(fluidPage(
titlePanel("test"),
sidebarLayout(
sidebarPanel(
helpText("text"),
select2Input("txt","stock",choices=c("AAPL","GOOG","MSFT"),selected=c("")),
actionButton("go","submit")
),
mainPanel(
tabsetPanel(type="tab",tabPanel("Plot",plotOutput("plot")),tabPanel("summary",tableOutput("table")),tabPanel("close",tableOutput("table1")))
)
)
))
server.R
# server.R
library(quantmod)
library(PerformanceAnalytics)
shinyServer(function(input, output) {
stockData <- new.env()
dataInput <- reactive({
if(input$go==0){return()} #confirming button click
else if(input$go==1){
validate(
need(input$txt != "", label = "stock")
)
getSymbols(input$txt, src = "yahoo", env=stockData,from =as.Date(input$dates) )
}
})
output$plot=plot chart.correlation
output$table=table of Close values of stocks etc Cl(Data)
output$table1=output of last close
}
)
以上闪亮的代码是不完整的,因为我不知道如何更进一步,并有一些逻辑错误。
1)我使用if(input$go==0){return()}
来验证按钮点击,但它只能使用一次
2)在shinyapps.io library(PerformanceAnalytics)
中部署此代码时会发生干扰并且不允许部署
我该如何解决这些问题?
答案 0 :(得分:1)
在深入了解shiny
之前,请查看他们页面上的教程,它们将涵盖进一步开发应用程序所需的大量材料。
#1
rm(list = ls())
library(shiny)
library(xts)
library(DT)
library(quantmod)
library(shinysky)
library(PerformanceAnalytics)
x <- list()
Data <- data.frame()
clo <- data.frame()
ui <- fluidPage(
titlePanel("test"),
tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),
sidebarLayout(
sidebarPanel(
helpText("text"),
select2Input("txt","stock",choices=c("AAPL","GOOG","MSFT"),selected=c("AAPL","GOOG")),
dateInput("dates", "Date:", value = "2016-01-01"),
actionButton("go","submit")
),
mainPanel(
tabsetPanel(type="tab",tabPanel("Plot",plotOutput("plot")),tabPanel("summary",dataTableOutput("table")),tabPanel("close",dataTableOutput("table1")))
)
)
)
server <- function(input, output) {
stockData <- new.env()
dataInput <- reactive({
if(input$go==0){return()} #confirming button click
isolate({
input$go
getSymbols(input$txt, src = "yahoo", env=stockData,from =as.Date(input$dates) )
Data <- data.frame()
validate(need(input$txt != "", label = "stock"))
for (i in 1:length(input$txt)) {
x[[i]] <- get(input$txt[i], pos=stockData) # get data from stockData environment
Data <- cbind(Data,diff(log(Cl(x[[i]]))))
}
Data
})
})
Last_Close <- reactive({
if(input$go==0){return()} #confirming button click
isolate({
input$go
validate(need(input$txt != "", label = "stock"))
for (i in 1:length(input$txt)) {
x[[i]] <- get(input$txt[i], pos=stockData) # get data from stockData environment
clo <- cbind(clo,Cl(x[[i]]))
}
clo
})
})
output$plot <- renderPlot(chart.Correlation(dataInput()))
output$table <- DT::renderDataTable(datatable(as.data.frame(Cl(dataInput()))))
output$table1 <- DT::renderDataTable(datatable(as.data.frame(Cl(Last_Close()))))
}
shinyApp(ui, server)