【发布时间】:2021-05-11 07:18:11
【问题描述】:
我正在编写一个 R 闪亮的应用程序。我面临很多麻烦,尤其是 checkboxGroupInput 函数。我希望我能够创建一个动态列表,该列表将自动列出除名为source_file 的数据集的第一列source_file$Date 之外的所有列,我对此并不完全确定。非常感谢您能提供的任何帮助!
source_file 的样本数据集如下所示:
| Date | Index 1 | Index 2 | Index 3 | Index 4 | Index 5 |
|---|---|---|---|---|---|
| 2016-01-01 | +5% | -2% | +5% | +10% | +12% |
| 2016-01-08 | +3% | +13% | -8% | -3% | +10% |
| 2016-01-15 | +2% | +11% | -3% | +4% | -15% |
最终目标是希望 checkboxGroupInput 函数能够自动读取从第二列开始的所有列(忽略日期)。在这种情况下,复选框将加载 5 个选项,索引 1 到索引 5。它应该是可复制的,以便它可以根据指定的数据加载任意数量的索引。我尝试对每个单独的索引进行硬编码,但这绝对是违反直觉的,而且非常令人沮丧。
tabPanel("Target Volatility Portfolio",
sidebarPanel(
tags$h3("Find an optimised portfolio to achieve maximum return for a given level of risk/volatility"),
tags$h4("Input:"),
checkboxGroupInput("portfolio_selection",
"Select Number of Indexes for Portfolio",
choices = list(#####please send help here#####)
编辑:如果您能帮我解决这个问题,将不胜感激。
我想以这种格式将来自复选框的输出引用到我的 global.R 中。基本上,我想使用选定的变量来绘制图表。选择 2 个变量将导致绘制与 2 个变量相关的图形,而选择 10 个变量将创建包含所有 10 个变量的图。 (我基本上是在绘制 x 股票数量的有效市场边界,其中 x 是所选变量的数量。它有点难以解释,但我希望附上代码可以为您提供一些见解)哈希线是我需要的帮助定影。谢谢!
plot_emf = function(n_points, target_vol, portfolio_selection)
{
first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]
#######asset_returns = source_file[first:last]########
# Extract necessary parameters
n_assets = ncol(asset_returns)
n_obs = nrow(asset_returns)
n_years = n_obs / 52
# Initialize containers for holding return and vol simulations
return_vector = c()
vol_vector = c()
sharpe_vector = c()
for (i in 1:n_points)
{
# Generate random weights for n assets from uniform(0,1)
asset_weights = runif(n_assets, min = 0, max = 1)
normalization_ratio = sum(asset_weights)
# Asset weights need to add up to 100%
asset_weights = asset_weights / normalization_ratio
# print(asset_weights)
# print(asset_returns)
# Generate the portfolio return vector using these weights
random_portfolio_returns = emf_portfolio_returns(
asset_weights,
asset_returns)
# print(random_portfolio_returns)
# plot_returns_histogram(random_portfolio_returns$portfolio_returns)
cumulative_return = calculate_cumulative_return(random_portfolio_returns$portfolio_returns)
annualized_return = 100*((1 + cumulative_return/100)^(1/n_years) - 1)
annualized_vol = sd(random_portfolio_returns$portfolio_returns)*(52^0.5)
sharpe = annualized_return / annualized_vol
return_vector = append(return_vector, annualized_return)
vol_vector = append(vol_vector, annualized_vol)
sharpe_vector = append(sharpe_vector, sharpe)
#print(paste("Asset weights:",asset_weights))
#print(paste("Anualized return:",annualized_return))
#print(paste("Annualized vol:",annualized_vol))
}
g = ggplot(data = data.frame(vol_vector, return_vector, sharpe_vector),
aes(x = vol_vector, y = return_vector, color = sharpe_vector)) +
scale_color_gradient(low = "red", high = "blue", name = "Sharpe Ratio\n(Return/Risk)") +
ggtitle("Efficient Market Frontier") +
xlab("Annualized Vol (%)") +
ylab("Annualized Return (%)") +
theme(plot.title = element_text(hjust=0.5)) + geom_vline(xintercept=target_vol) +
geom_point()
print(g)
}
【问题讨论】: