将 checkboxGroupInput 的结果引用到 R Shiny 上的绘图中
Referencing checkboxGroupInput's results into plot on R Shiny
我遇到了一行问题(在 Global.R 中用下面的散列表示)。我目前正在开发与财务相关的 R 闪亮应用程序。其中一个选项卡包含一个 checkboxgGroupInput
,它列出了我的源文件的所有列,除了 Date,这是 stocks/indexes 的名称,我可以 select 将其包含在下面的 ggplot 中。
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%
在这种情况下,复选框将加载 5 个选项,索引 1 到索引 5。我设想我将能够 select 索引 2 和 3,这将生成一个涉及2 个变量 selected(索引 2 和 3)。但是,我不确定我应该如何参考 checkboxGroupInput
的输出编写代码
我尝试执行以下操作以引用 checkboxGroupInput
输出的第一个和最后一个值,但它似乎不起作用。
first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]
asset_returns = source_file[[first:last]]
我希望有人能帮我找出引用这些值以实现我的情节的正确方法。我在下面附上了我的 App.R 和 Global.R(我简化了原始代码的代码,因为我不想用超过 150 行代码来淹没这个问题,但逻辑仍然可以正常工作)谢谢你太厉害了!
App.R
# Load packages
library(shiny)
library(shinythemes)
source("./global.R")
# Defining UI
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
"Test App",
tabPanel("Target Plot",
sidebarPanel(
tags$h4("Input:"),
checkboxGroupInput("portfolio_selection",
"Select Number of Indexes for Portfolio",
choices = list()),
), #sidebarpanel
mainPanel(
plotOutput(outputId = "target_plot"),
), #mainPanel
) #tabpanel
) #navbarPage
) #fluidPage
server <- function(input, output, session) {
#to output target_plot for Target plot
output$target_plot <- renderPlot({
plot_emf(portfolio_selection = input$portfolio_selection)
})
# Column names we want to show - all except `Date`
opts <- setdiff(colnames(source_file), "Date")
# Update checkboxGroupInput with variables in source_file:
updateCheckboxGroupInput(
session, "portfolio_selection", choices = opts)
}
# Create Shiny Object
shinyApp(ui = ui, server = server)
Global.R
# Load packages
library(tidyverse)
library(ggcorrplot)
library(zoo)
library(xts)
library(testit)
#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")
#########################
plot_emf = function(portfolio_selection)
{
first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]
asset_returns = source_file[[first:last]] ###################issue with this line###################
g = ggplot(data = asset_returns),
aes(x =Date, y=asset_returns)) +
ggtitle("Efficient Market Frontier") +
geom_point()
print(g)
}
尝试将 plot_emf
函数更改为:
plot_emf = function(portfolio_selection)
{
source_file %>%
select(Date, portfolio_selection) %>%
pivot_longer(cols = -Date) %>%
ggplot() + aes(x =Date, y=value, color = name) +
ggtitle("Efficient Market Frontier") +
geom_point()
}
我遇到了一行问题(在 Global.R 中用下面的散列表示)。我目前正在开发与财务相关的 R 闪亮应用程序。其中一个选项卡包含一个 checkboxgGroupInput
,它列出了我的源文件的所有列,除了 Date,这是 stocks/indexes 的名称,我可以 select 将其包含在下面的 ggplot 中。
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% |
在这种情况下,复选框将加载 5 个选项,索引 1 到索引 5。我设想我将能够 select 索引 2 和 3,这将生成一个涉及2 个变量 selected(索引 2 和 3)。但是,我不确定我应该如何参考 checkboxGroupInput
我尝试执行以下操作以引用 checkboxGroupInput
输出的第一个和最后一个值,但它似乎不起作用。
first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]
asset_returns = source_file[[first:last]]
我希望有人能帮我找出引用这些值以实现我的情节的正确方法。我在下面附上了我的 App.R 和 Global.R(我简化了原始代码的代码,因为我不想用超过 150 行代码来淹没这个问题,但逻辑仍然可以正常工作)谢谢你太厉害了!
App.R
# Load packages
library(shiny)
library(shinythemes)
source("./global.R")
# Defining UI
ui <- fluidPage(theme = shinytheme("darkly"),
navbarPage(
"Test App",
tabPanel("Target Plot",
sidebarPanel(
tags$h4("Input:"),
checkboxGroupInput("portfolio_selection",
"Select Number of Indexes for Portfolio",
choices = list()),
), #sidebarpanel
mainPanel(
plotOutput(outputId = "target_plot"),
), #mainPanel
) #tabpanel
) #navbarPage
) #fluidPage
server <- function(input, output, session) {
#to output target_plot for Target plot
output$target_plot <- renderPlot({
plot_emf(portfolio_selection = input$portfolio_selection)
})
# Column names we want to show - all except `Date`
opts <- setdiff(colnames(source_file), "Date")
# Update checkboxGroupInput with variables in source_file:
updateCheckboxGroupInput(
session, "portfolio_selection", choices = opts)
}
# Create Shiny Object
shinyApp(ui = ui, server = server)
Global.R
# Load packages
library(tidyverse)
library(ggcorrplot)
library(zoo)
library(xts)
library(testit)
#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")
#########################
plot_emf = function(portfolio_selection)
{
first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]
asset_returns = source_file[[first:last]] ###################issue with this line###################
g = ggplot(data = asset_returns),
aes(x =Date, y=asset_returns)) +
ggtitle("Efficient Market Frontier") +
geom_point()
print(g)
}
尝试将 plot_emf
函数更改为:
plot_emf = function(portfolio_selection)
{
source_file %>%
select(Date, portfolio_selection) %>%
pivot_longer(cols = -Date) %>%
ggplot() + aes(x =Date, y=value, color = name) +
ggtitle("Efficient Market Frontier") +
geom_point()
}