Here is my code I have been using but I need to change it in order to get my app to start functioning properly. Please help
title: “Portfolio Analysis App”
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
<code>#Instal and Load R packages
library(tidyverse)
library(highcharter)
library(tidyquant)
library(timetk)
library(flexdashboard)
library(quantmod)
library(dplyr)
library(purrr)
library(scales)
</code>
<code>#Instal and Load R packages
library(tidyverse)
library(highcharter)
library(tidyquant)
library(timetk)
library(flexdashboard)
library(quantmod)
library(dplyr)
library(purrr)
library(scales)
</code>
#Instal and Load R packages
library(tidyverse)
library(highcharter)
library(tidyquant)
library(timetk)
library(flexdashboard)
library(quantmod)
library(dplyr)
library(purrr)
library(scales)
<code>#Fetch stock prices for specified symbols triggered by user input.
prices <- eventReactive(input$go, {
symbols <- c(input$stock1, input$stock2, input$stock3, input$stock4, input$stock5)
prices <-
getSymbols(symbols,
src = 'yahoo',
from = input$date,
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
})
</code>
<code>#Fetch stock prices for specified symbols triggered by user input.
prices <- eventReactive(input$go, {
symbols <- c(input$stock1, input$stock2, input$stock3, input$stock4, input$stock5)
prices <-
getSymbols(symbols,
src = 'yahoo',
from = input$date,
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
})
</code>
#Fetch stock prices for specified symbols triggered by user input.
prices <- eventReactive(input$go, {
symbols <- c(input$stock1, input$stock2, input$stock3, input$stock4, input$stock5)
prices <-
getSymbols(symbols,
src = 'yahoo',
from = input$date,
auto.assign = TRUE,
warnings = FALSE) %>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
})
<code>symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
# Define a reactive expression for asset returns calculation
asset_returns_xts <- eventReactive(input$go, {
prices <- prices()
w <- c(input$w1/100, input$w2/100, input$w3/100,
input$w4/100, input$w5/100)
asset_returns_xts <- prices %>%
to.period(period = input$rebalance, indexAt = "last", OHLC = FALSE) %>%
tk_tbl(preserve_index = TRUE, rename_index = "date") %>%
gather(asset, returns, -date) %>%
group_by(asset) %>%
mutate(returns = (log(returns) - log(lag(returns)))) %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w,
col_rename = "returns",
rebalance_on = input$rebalance) %>%
na.omit()
})
</code>
<code>symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
# Define a reactive expression for asset returns calculation
asset_returns_xts <- eventReactive(input$go, {
prices <- prices()
w <- c(input$w1/100, input$w2/100, input$w3/100,
input$w4/100, input$w5/100)
asset_returns_xts <- prices %>%
to.period(period = input$rebalance, indexAt = "last", OHLC = FALSE) %>%
tk_tbl(preserve_index = TRUE, rename_index = "date") %>%
gather(asset, returns, -date) %>%
group_by(asset) %>%
mutate(returns = (log(returns) - log(lag(returns)))) %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w,
col_rename = "returns",
rebalance_on = input$rebalance) %>%
na.omit()
})
</code>
symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
# Define a reactive expression for asset returns calculation
asset_returns_xts <- eventReactive(input$go, {
prices <- prices()
w <- c(input$w1/100, input$w2/100, input$w3/100,
input$w4/100, input$w5/100)
asset_returns_xts <- prices %>%
to.period(period = input$rebalance, indexAt = "last", OHLC = FALSE) %>%
tk_tbl(preserve_index = TRUE, rename_index = "date") %>%
gather(asset, returns, -date) %>%
group_by(asset) %>%
mutate(returns = (log(returns) - log(lag(returns)))) %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w,
col_rename = "returns",
rebalance_on = input$rebalance) %>%
na.omit()
})
Asset Returns
<code>#Visualization of Portfolio Returns
renderHighchart({
req(input$go) # Ensure actionButton is clicked
asset_returns_xts <- asset_returns_xts()
highchart(type = "stock") %>%
hc_title(text = "Log Returns", style = list(color = "#333333", fontSize = "18px", fontWeight = "bold")) %>%
hc_add_series(asset_returns_xts[, symbols[1]], name = symbols[1], color = "#3498DB") %>%
hc_add_series(asset_returns_xts[, symbols[2]], name = symbols[2], color = "#FF6347") %>%
hc_add_series(asset_returns_xts[, symbols[3]], name = symbols[3], color = "#20B2AA") %>%
hc_add_series(asset_returns_xts[, symbols[4]], name = symbols[4], color = "#FFD700") %>%
hc_add_series(asset_returns_xts[, symbols[5]], name = symbols[5], color = "#C71585") %>%
hc_yAxis(
title = list(text = "Log Returns", style = list(color = "#333333", fontWeight = "bold")),
labels = list(format = "{value}%"),
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_xAxis(
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_chart(backgroundColor = "#FFFFFF") %>%
hc_legend(align = "center", verticalAlign = "bottom", layout = "horizontal") %>%
hc_navigator(enabled = TRUE) %>%
hc_scrollbar(enabled = TRUE) %>%
hc_exporting(enabled = TRUE) %>%
hc_tooltip(shared = TRUE, crosshairs = TRUE) %>%
hc_rangeSelector(buttonTheme = list(
fill = '#333333',
stroke = '#000000',
style = list(color = '#FFFFFF'),
states = list(
hover = list(
fill = '#555555',
style = list(color = 'white')
),
select = list(
fill = '#000000',
style = list(color = '#C0C0C0')
)
)
)) %>%
hc_add_theme(hc_theme_flat())
})
</code>
<code>#Visualization of Portfolio Returns
renderHighchart({
req(input$go) # Ensure actionButton is clicked
asset_returns_xts <- asset_returns_xts()
highchart(type = "stock") %>%
hc_title(text = "Log Returns", style = list(color = "#333333", fontSize = "18px", fontWeight = "bold")) %>%
hc_add_series(asset_returns_xts[, symbols[1]], name = symbols[1], color = "#3498DB") %>%
hc_add_series(asset_returns_xts[, symbols[2]], name = symbols[2], color = "#FF6347") %>%
hc_add_series(asset_returns_xts[, symbols[3]], name = symbols[3], color = "#20B2AA") %>%
hc_add_series(asset_returns_xts[, symbols[4]], name = symbols[4], color = "#FFD700") %>%
hc_add_series(asset_returns_xts[, symbols[5]], name = symbols[5], color = "#C71585") %>%
hc_yAxis(
title = list(text = "Log Returns", style = list(color = "#333333", fontWeight = "bold")),
labels = list(format = "{value}%"),
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_xAxis(
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_chart(backgroundColor = "#FFFFFF") %>%
hc_legend(align = "center", verticalAlign = "bottom", layout = "horizontal") %>%
hc_navigator(enabled = TRUE) %>%
hc_scrollbar(enabled = TRUE) %>%
hc_exporting(enabled = TRUE) %>%
hc_tooltip(shared = TRUE, crosshairs = TRUE) %>%
hc_rangeSelector(buttonTheme = list(
fill = '#333333',
stroke = '#000000',
style = list(color = '#FFFFFF'),
states = list(
hover = list(
fill = '#555555',
style = list(color = 'white')
),
select = list(
fill = '#000000',
style = list(color = '#C0C0C0')
)
)
)) %>%
hc_add_theme(hc_theme_flat())
})
</code>
#Visualization of Portfolio Returns
renderHighchart({
req(input$go) # Ensure actionButton is clicked
asset_returns_xts <- asset_returns_xts()
highchart(type = "stock") %>%
hc_title(text = "Log Returns", style = list(color = "#333333", fontSize = "18px", fontWeight = "bold")) %>%
hc_add_series(asset_returns_xts[, symbols[1]], name = symbols[1], color = "#3498DB") %>%
hc_add_series(asset_returns_xts[, symbols[2]], name = symbols[2], color = "#FF6347") %>%
hc_add_series(asset_returns_xts[, symbols[3]], name = symbols[3], color = "#20B2AA") %>%
hc_add_series(asset_returns_xts[, symbols[4]], name = symbols[4], color = "#FFD700") %>%
hc_add_series(asset_returns_xts[, symbols[5]], name = symbols[5], color = "#C71585") %>%
hc_yAxis(
title = list(text = "Log Returns", style = list(color = "#333333", fontWeight = "bold")),
labels = list(format = "{value}%"),
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_xAxis(
gridLineColor = "#D8D8D8",
gridLineDashStyle = "Dash"
) %>%
hc_chart(backgroundColor = "#FFFFFF") %>%
hc_legend(align = "center", verticalAlign = "bottom", layout = "horizontal") %>%
hc_navigator(enabled = TRUE) %>%
hc_scrollbar(enabled = TRUE) %>%
hc_exporting(enabled = TRUE) %>%
hc_tooltip(shared = TRUE, crosshairs = TRUE) %>%
hc_rangeSelector(buttonTheme = list(
fill = '#333333',
stroke = '#000000',
style = list(color = '#FFFFFF'),
states = list(
hover = list(
fill = '#555555',
style = list(color = 'white')
),
select = list(
fill = '#000000',
style = list(color = '#C0C0C0')
)
)
)) %>%
hc_add_theme(hc_theme_flat())
})
Sidebar {.sidebar}
<code>
fluidRow(
column(6,
textInput("stock1", "Stock 1", "SPY")),
column(6,
numericInput("w1", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock2", "Stock 2", "EFA")),
column(6,
numericInput("w2", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock3", "Stock 3", "IJS")),
column(6,
numericInput("w3", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock4", "Stock 4", "EEM")),
column(6,
numericInput("w4", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock5", "Stock 5", "AGG")),
column(6,
numericInput("w5", "Portf. %", 10, min = 1, max = 100))
)
fluidRow(
column(12,
dateInput("date", "Starting Date", "2013-01-01", format = "yyyy-mm-dd"))
)
fluidRow(
column(12,
selectInput("rebalance", "Rebalance Frequency",
c("Monthly" = "months",
"Weekly" = "weeks",
"Daily" = "days"))
)
)
fluidRow(
column(12,
sliderInput("window", "Window",
min = 3, max = 36, value = 24, step = 1)
)
)
actionButton("go", "Submit")
</code>
<code>
fluidRow(
column(6,
textInput("stock1", "Stock 1", "SPY")),
column(6,
numericInput("w1", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock2", "Stock 2", "EFA")),
column(6,
numericInput("w2", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock3", "Stock 3", "IJS")),
column(6,
numericInput("w3", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock4", "Stock 4", "EEM")),
column(6,
numericInput("w4", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock5", "Stock 5", "AGG")),
column(6,
numericInput("w5", "Portf. %", 10, min = 1, max = 100))
)
fluidRow(
column(12,
dateInput("date", "Starting Date", "2013-01-01", format = "yyyy-mm-dd"))
)
fluidRow(
column(12,
selectInput("rebalance", "Rebalance Frequency",
c("Monthly" = "months",
"Weekly" = "weeks",
"Daily" = "days"))
)
)
fluidRow(
column(12,
sliderInput("window", "Window",
min = 3, max = 36, value = 24, step = 1)
)
)
actionButton("go", "Submit")
</code>
fluidRow(
column(6,
textInput("stock1", "Stock 1", "SPY")),
column(6,
numericInput("w1", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock2", "Stock 2", "EFA")),
column(6,
numericInput("w2", "Portf. %", 25, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock3", "Stock 3", "IJS")),
column(6,
numericInput("w3", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock4", "Stock 4", "EEM")),
column(6,
numericInput("w4", "Portf. %", 20, min = 1, max = 100))
)
fluidRow(
column(6,
textInput("stock5", "Stock 5", "AGG")),
column(6,
numericInput("w5", "Portf. %", 10, min = 1, max = 100))
)
fluidRow(
column(12,
dateInput("date", "Starting Date", "2013-01-01", format = "yyyy-mm-dd"))
)
fluidRow(
column(12,
selectInput("rebalance", "Rebalance Frequency",
c("Monthly" = "months",
"Weekly" = "weeks",
"Daily" = "days"))
)
)
fluidRow(
column(12,
sliderInput("window", "Window",
min = 3, max = 36, value = 24, step = 1)
)
)
actionButton("go", "Submit")
I tried to run the app expecting the graph to show but nothing happens
New contributor
Will Campbell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.