簡體   English   中英

R閃亮反應性圖

[英]R shiny reactive plotly graph

我想在有光澤的圖形上繪制一個非常簡單的圖形...但是我不明白...這是一個燭台圖形...我從Yahoo Finance加載數據,將其放在列表中並創建了一個遵循我們想要看到的數據框...但是它不起作用,它將加載除以下語句外的所有圖形:“第一個參數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")
  })


})

如果有人發現我做錯了...

嗨,您的代碼首先遇到了幾個問題, 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返回數據幀,而是從列名的向量返回。 我剛剛在sortie結束時添加了對temp的調用以解決此問題。 瑞安(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")
  })


}

就是這樣,但我在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")
  })


})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM