I have written a code for calculating correlation of stocks in R and this is the code.
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)
But I am getting a hard time converting this into a shiny app
This is so far i have done in my shiny app. I have selected shinysky
library for selecting multiple stocks
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
}
)
The above shiny code is incomplete because I have no idea how to go further and have some logic error.
1) I have used if(input$go==0){return()}
to validate button click but it only work once
2) while deploying this code in shinyapps.io library(PerformanceAnalytics)
interfere and doesn't allow deployment
How can I solve these problems?
Before diving into shiny
please look into the tutorials on their page, they will cover a lot of material much needed to further develop your apps.
For #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)