Search code examples
rggplot2shinyfinancecheckboxlist

Referencing checkboxGroupInput's results into plot on R Shiny


I am facing problems with a line (indicated with hashes below in Global.R). I'm currently working on a finance related R shiny app. One of the tabs includes a checkboxgGroupInput which lists all the columns of my source file except Date, which are the names of stocks/indexes which I can select to be included in the ggplot below.

Sample dataset of source_file would look something like this:

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%

In this case, the check box would load up 5 options, Index 1 to Index 5. I envision that I will be able to select Index 2 and 3, and that would generate a plot involving the 2 variables selected (Index 2 and 3). However, I'm unsure as to how I should be writing the code with reference to the output of checkboxGroupInput

I tried to do the following to reference the first and last values of the output from checkboxGroupInput but it seems to not be working.

first <- portfolio_selection[1]
last <- portfolio_selection[length(portfolio_selection)]

asset_returns = source_file[[first:last]]

I hope someone could help me figure out the proper method of referencing the values to achieve my plot. I attached my App.R and Global.R below (I simplified the code from my original code as I don't want to flood the question with over 150 lines of code, but the logic still works as fine) Thank you so much!

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) 
}

Solution

  • Try changing plot_emf function to :

    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()
    }