在 shiny 中使用 quantmod 的多个股票

Multiple stock using quantmod in shiny

我在 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) 干扰并且不允许部署

如何解决这些问题?

在深入研究 shiny 之前,请查看其页面上的教程,它们将涵盖 material 进一步开发您的应用程序所需的大量内容。

对于#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)