【问题标题】:Listing All Variables (Column Names) in R Shiny's checkboxGroupInput在 R Shiny 的 checkboxGroupInput 中列出所有变量(列名)
【发布时间】: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) 
} 

【问题讨论】:

    标签: r list shiny


    【解决方案1】:

    您可以尝试使用colnames() 提取新选项,然后将checkboxGroupInput 更新为updateCheckboxGroupInput()

    server <- function(input, output, session) {
      
      # Read the data once per session - this step might be better to
      # put in a `global.R` file
      source_file <- read.csv("source_file.csv")
      
      # Column names we want to show - all except `Date`
      opts <- setdiff(colnames(source_file), "Date") 
     
      # Update your checkboxGroupInput:
      updateCheckboxGroupInput(
        session, "portfolio_selection", choices = opts
      )
       
      # Rest of app after this point  --------------------------------------
    
    }
    

    【讨论】:

    • 感谢您的帮助,这为我解决了 1 个问题哈哈!如果您能再次提供帮助,我还有一个问题,非常感谢。我将其添加到我的原始帖子中,这就是我希望实现的。它真的会为我完成代码,我希望你能明白我对上面示例代码的解释。非常感谢!
    • 没问题。我看过你的编辑,但我不完全确定你在问什么。你能澄清一下具体的意图是什么吗?
    • 我只是附上代码并试图解释它。非常感谢您迄今为止提供的所有帮助,这真的很重要!
    • 嗯,老实说,我认为这是一个足够大的问题,它应该有自己的帖子,因为这需要反应性和很多东西第一个答案不需要触及。像这样的东西(使用dplyr)可能会让你开始:cut_down_data&lt;- reactive(df %&gt;% select(seq(which(colnames(df) == input$portfolio_selection))))。然后你可以在你的绘图代码中引用cut_down_data(),尽管如上所述,你需要知道一些关于反应性的知识才能做到这一点。
    • 好的,非常感谢您在这方面的帮助和指导!没有你真的不会走到这一步:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多