[英]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.