Hello,
I was trying to implement and modify the code for the first app that I found in Reproducible Finance. When I run the app, the UI appears but the Server does not allow for any of the inputs to function. In the following code, could someone help me see where I am going wrong?
This is a Shiny web application. You can run the application by clicking
the 'Run App' button above.
Find out more about building applications with Shiny here:
http://shiny.rstudio.com/
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(shinydashboard)
library(tidyverse)
library(tidyquant)
library(timetk)
library(markdown)
library(Quandl)
library(xts)
library(forecast)
library(highcharter)
library(devtools)
library(config)
library(lubridate)
#tq_index_options()
library(readxl)
library(data.table)
library(dygraphs)
library(purrr)
library(quantmod)
library(PerformanceAnalytics)
library(tibbletime)
Quandl.api_key("iGhm2Z5DJg4YE4oG23ym")
ui <- fluidPage(
Application title
titlePanel("Portfolio Returns"),
sidebarPanel(
fluidRow(
column(6,
textInput("stock1", "Stock 1", "SPY")),
column(5,numericInput("w1", "Portf. %", 25, min =1, max = 100))),
fluidRow(
column(6,
textInput("stock2", "Stock 2", "EFA")),
column(5,numericInput("w1", "Portf. %", 25, min =1, max = 100))),
fluidRow(
column(6,
textInput("stock3", "Stock 3", "IJS")),
column(5,numericInput("w1", "Portf. %", 20, min =1, max = 100))),
fluidRow(
column(6,
textInput("stock4", "Stock 4", "EEM")),
column(5,numericInput("w1", "Portf. %", 20, min =1, max = 100))),
fluidRow(
column(6,
textInput("stock5", "Stock 5", "AGG")),
column(5,numericInput("w1", "Portf. %", 10, min =1, max = 100))),
fluidRow(
column(7,
dateInput("date","Starting Date", "2013-01-01", format = "yyyy-mm-dd"))),
fluidRow(
column(6,
selectInput("rebalance", "rebal freq",
c("Yearly" = "years",
"Monthly"="months",
"Weekly"="weeks")))),
actionButton("go", "Submit")
))
mainPanel(tabsetPanel(
tabPanel("Plot", plotOutput("plot")),
tabPanel("plot2", plotOutput("plot2")),
tabPanel("plot3", plotOutput("plot3"))
)
)
Define server logic required to draw a histogram
server <- function(input, output) {
portfolio_returns_byhand<- eventReactive(input$go, {
#####Maybe problem here###########################################
symbols <- c(input$stock1, input$stock2, input$stock3,input$stock4, input$stock5)
prices <- symbols %>%
tq_get(get = "quandl",
from = "2007-01-01",
to = "2020-05-31",
transform = "rdiff",
collapse = "monthly",
column_index = 11) %>%
rename(monthly.returns = adj.close)
prices
#prices <- read_csv("Reproducible Finance.csv",
# col_types = cols(date = col_date(format = "%m/%d/%Y"))) %>% tk_xts(date_var = date)
w <- c(input$w1/100,input$w2/100,input$w3/100,input$w4/100,input$w5/100)
asset_returns_long <-
prices %>% to.monthly(indexAt = "last", OHLC=FALSE) %>% tk_tbl(perserve_index = TRUE, rename_index = "date") %>%
gather(asset, returns,-date) %>% group_by(asset) %>% mutate(returns = (log(returns)- log(lag(returns))))
portfolio_returns_byhand<- asset_returns_long %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w,
col_rename= "returns")
})
output$plot <- renderPlot({
portfolio_returns_byhand() %>% ggplot(aes(x = returns))
ggplot(aes(x = return)) + geom_histogram(alpha = 0.25, binwidth = .01, fill = "cornflowerblue")
})
output$plot2 <- renderPlot({
portfolio_returns_byhand()%>% ggplot(aes(x = returns)) + geom_density(
size=1,
color= "blue"
)
})
output$plot3 <- renderPlot({
portfolio_returns_byhand() %>% ggplot(aes(x = returns)) + geom_histogram(alpha = 0.25,binwidth = 0.01, fill = "blue")+
geom_density(
size=1,
color = "red")
})
}
Run the application
shinyApp(ui = ui, server = server)