R闪亮的反应性情节图

时间:2017-12-22 16:27:44

标签: r

我想制作一个有光泽,非常简单的情节图...但我不明白......这是一个烛台图...我从雅虎财务加载数据,我把它放在一个列表中我按照我们想要的内容创建了一个数据框...但它不起作用,除了带有句子的图形之外它加载所有: “第一个参数data必须是数据框或共享数据”

library(shiny)
library(quantmod)
library(lubridate)
library(plotly)
library(dplyr)
trim<-Sys.Date()- months(3)
#floor_date(ajd,"month")

comp<-c("CAC 40","Total","Sanofi","BNP","LVMH","Airbus","Axa","L'Oreal","Air Liquide","Danone","Vinci","Schneider","Societe Generale","Kering","Orange")
ref<-data.frame("^FCHI","FP.PA","SAN.PA","BNP.PA","MC.PA","AIR.PA","CS.PA","OR.PA","AI.PA","BN.PA","DG.PA","SU.PA","GLE.PA","KER.PA","ORA.PA")
colnames(ref)<-comp

for (i in 1:length(comp)){
  stock<-ref[1,i]
  stock<-as.character(stock)
  getSymbols(stock,src="yahoo",from=trim,to=Sys.Date())
}
for (i in 1:length(comp)){
  ref[,i]<-as.character(ref[,i])
}
ref[,1]<-c("FCHI")

data<-list()
for (i in 1:length(comp)){
  data[[i]]<-get(ref[,i])
}



# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Top companies of CAC 40 Analysis"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      h1("Companies"),
      selectInput("titre","Company:",
                  choice=colnames(ref)),
      hr(),
      helpText("Data from yahoo finance")
    ),

    # Show a plot of the generated distribution
    mainPanel(
      h3("Evolution du cours"),
       plotlyOutput("graph")
    )
  )
))


library(shiny)
library(quantmod)
library(lubridate)
library(plotly)
library(dplyr)



# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  sortie<-reactive({
    compa<-input$titre
    temp<-data.frame(Date=index(data[[compa]]),coredata(data[[compa]]))
    colnames(temp)<-c("Date","Open","High","Low","Close","Volume","Adjusted")
  })

  output$graph <- renderPlotly({
    plot_ly(sortie,x=sortie$Date,type="candlestick",
            open=sortie$Open,close=sortie$Close,high=sortie$High,low=sortie$Low)
    layout(title="Quaterly evolution")
  })


})

如果有人发现我做错了......

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题  首先data不是命名列表所以我改变了行

temp<-data.frame(Date=index(data[[compa]]),coredata(data[[compa]]))

temp<-data.frame(Date=index(data[[which(compa == comp)]]),coredata(data[[which(compa == comp) ]]))

获取comnp

的正确索引

然后你没有从sortie返回数据框,而是返回列名的向量。我刚刚在temp的末尾添加了对sortie的调用来解决此问题。 Ryan在他的评论中已经提到的最后一件事sortie之后的括号。下面是服务器代码的工作版本。我没有改变任何其他事情。

function(input, output) {

  Sortie<-reactive({
    compa<-input$titre
    temp<-data.frame(Date=index(data[[which(compa == comp)]]),coredata(data[[which(compa == comp) ]]))
    colnames(temp)<-c("Date","Open","High","Low","Close","Volume","Adjusted")
    temp
  })

  output$graph <- renderPlotly({
    sortie <- Sortie()
    plot_ly(sortie,x=sortie$Date,type="candlestick",
            open=sortie$Open,close=sortie$Close,high=sortie$High,low=sortie$Low) %>% 
      layout(title="Quaterly evolution")
  })


}

答案 1 :(得分:1)

就是这样,但我在ui代码中添加了列表中的公司名称:

data<-list()
for (i in 1:length(comp)){
  data[[i]]<-get(ref[,i])
}
names(data)<-comp

所以在我的原始代码使用之后:

shinyServer(function(input, output) {

  sortie<-reactive({
    compa<-input$titre
    temp<-data.frame(Date=index(data[[compa]]),coredata(data[[compa ]]))
    colnames(temp)<-c("Date","Open","High","Low","Close","Volume","Adjusted")
    temp
    })

  output$graph <- renderPlotly({
    sortie<-sortie()
    plot_ly(sortie,x=~Date,type="candlestick",
            open=~Open,close=~Close,high=~High,low=~Low)%>%
            layout(title="Quarterly evolution")
  })


})