我正在尝试创建一个与金融相关的 R Shiny 应用程序。截至目前,我有一个用于创建自定义投资组合的选项卡,这需要我为numericInput
原始source_file
. 示例source_file
如下所示:
日期 | 沪深500 | 上海 | CSI300 | 恒指 | 性病 |
---|---|---|---|---|---|
2016-01-01 | +5% | -2% | +5% | +10% | +12% |
2016-01-08 | +3% | +13% | -8% | -3% | +10% |
2016-01-15 | +2% | +11% | -3% | +4% | -15% |
目前,我必须numericInput
为每个变量手动硬编码每个框,如下所示:
tabPanel("Custom Portfolio by Weight",
sidebarPanel(
tags$h3("Create your own Custom Portfolio by Asset Weight"),
tags$h4("Input:"),
numericInput(inputId = "custom_csi500", "CSI500 (%)", min = 0, max = 100),
numericInput(inputId = "custom_shanghai", "Shanghai Stock Exchange (%)", min = 0, max = 100),
numericInput(inputId = "custom_csi300", "CSI300 (%)", min = 0, max = 100),
numericInput(inputId = "custom_hsi", "HSI (%)", min = 0, max = 100),
numericInput(inputId = "custom_sti", "STI (%)", min = 0, max = 100),
numericInput(inputId = "custom_twse", "TWSE (%)", min = 0, max = 100),
numericInput(inputId = "custom_msciw", "MSCI World (%)", min = 0, max = 100),
numericInput(inputId = "custom_sp500", "S&P500 (%)", min = 0, max = 100),
numericInput(inputId = "custom_n225", "Nikkei 225 (%)", min = 0, max = 100),
numericInput(inputId = "custom_lse", "London Stock Exchange (%)", min = 0, max = 100),
numericInput(inputId = "custom_asx", "ASX (%)", min = 0, max = 100),
但是,我想创建一些可以扩展到具有我指定的不同数量变量的任何数据帧的东西,而不必手动硬编码它们。我希望任何人都能够帮助我编写一个能够阅读的代码我的数据框中的变量数(日期列除外)并numericInput
为每个变量创建那么多框。非常感谢您的帮助!如果需要,我在下面附上了我的 App.R 和 Global.R 以供参考。干杯!
应用程序
# Load packages
library(shiny)
library(shinythemes)
source("./global.R")
# Defining UI
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
"Test App",
tabPanel("Custom Portfolio by Weight",
sidebarPanel(
tags$h3("Create your own Custom Portfolio by Asset Weight"),
tags$h4("Input:"),
numericInput(inputId = "custom_csi500", "CSI500 (%)", min = 0, max = 100),
numericInput(inputId = "custom_shanghai", "Shanghai Stock Exchange (%)", min = 0, max = 100),
numericInput(inputId = "custom_csi300", "CSI300 (%)", min = 0, max = 100),
numericInput(inputId = "custom_hsi", "HSI (%)", min = 0, max = 100),
numericInput(inputId = "custom_sti", "STI (%)", min = 0, max = 100),
numericInput(inputId = "custom_twse", "TWSE (%)", min = 0, max = 100),
numericInput(inputId = "custom_msciw", "MSCI World (%)", min = 0, max = 100),
numericInput(inputId = "custom_sp500", "S&P500 (%)", min = 0, max = 100),
numericInput(inputId = "custom_n225", "Nikkei 225 (%)", min = 0, max = 100),
numericInput(inputId = "custom_lse", "London Stock Exchange (%)", min = 0, max = 100),
numericInput(inputId = "custom_asx", "ASX (%)", min = 0, max = 100),
mainPanel(
plotOutput(outputId = "custom_returns"),
), #mainPanel
), #tabpanel
) #navbarPage
) #fluidPage
server <- function(input, output, session) {
#to output custom_returns for Custom Portfolio by Asset Weight
output$custom_returns <- renderPlot({
calculate_portfolio_returns(
customrange = input$customrange,
asset_weights = c(input$custom_csi500/100,
input$custom_shanghai/100,
input$custom_csi300/100,
input$custom_hsi/100,
input$custom_sti/100,
input$custom_twse/100,
input$custom_msciw/100,
input$custom_sp500/100,
input$custom_n225/100,
input$custom_lse/100,
input$custom_asx/100,
input$custom_custom/100))
})
}
# Create Shiny Object
shinyApp(ui = ui, server = server)
全球.R
# Load packages
library(tidyverse)
library(ggcorrplot)
library(zoo)
library(xts)
library(testit)
library(PerformanceAnalytics)
#choose source file to work with
file_name = file.choose()
source_file = read_csv(file_name)
source_file$Date = as.Date(source_file$Date, format = "%Y-%m-%d")
########################
calculate_portfolio_returns = function(customrange, asset_weights)
{
#filter source file by date range specified
source_file_filtered <- source_file %>%
filter(Date >= customrange[1] & Date <= customrange[2])
source_file_filtered_no_date <- source_file_filtered[,2:length(source_file_filtered)]
#create new column called Cumulative_Returns to convert % daily returns
Cumulative_Returns <- cumsum(source_file_filtered_no_date)
# Extract necessary parameters
n_cols = ncol(Cumulative_Returns)
n_assets = n_cols
#To ensure portfolio is always weighted at 100% at all times
assert(length(asset_weights) == n_assets)
assert(abs(sum(asset_weights)-1) <= 0.001)
portfolio_returns = data.matrix(Cumulative_Returns) %*% asset_weights
portfolio_returns_with_date = cbind(source_file_filtered[,1], portfolio_returns)
g = ggplot(data = portfolio_returns_with_date, mapping = aes(x=Date, y=portfolio_returns)) +
geom_line(color="blue") + ggtitle(paste("Custom Portfolio Returns from", customrange[1], "to", customrange[2])) +
geom_hline(yintercept = 0, linetype = "dashed") + theme(plot.title = element_text(hjust=0.5)) +
ylab("Portfolio Returns (%)")
print(g)
}