Reputation: 113
I would like to make a plotly graph on shiny, very simple... but i don't get it... it's a candlestick graph... I load data from yahoo finance, i put it in a list and i create a dataframe following what we want see... but it doesn't work, it load all except the graph with the sentence :
"First argument, data
, must be a data frame or shared 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")
})
})
If someone find something i made wrong...
Upvotes: 1
Views: 4124
Reputation: 113
It was that but i added the names of companies in the list in ui code :
data<-list()
for (i in 1:length(comp)){
data[[i]]<-get(ref[,i])
}
names(data)<-comp
So after my original code works with that :
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")
})
})
Upvotes: 1
Reputation: 5003
Hi there wasa a couple of problems with your code
first the data
was not a named list so I changed the line
temp<-data.frame(Date=index(data[[compa]]),coredata(data[[compa]]))
to
temp<-data.frame(Date=index(data[[which(compa == comp)]]),coredata(data[[which(compa == comp) ]]))
to get the right index of comnp
then you were not returning the data frame from sortie
but rather the vector of the column names. I just added a call to temp
at the end of sortie
to fix this. The last thing Ryan already mentioned in his comment with the brackets after sortie
. Below follows a working version of the server code. I haven't changed anything else.
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")
})
}
Upvotes: 1