1

I was hoping someone here could help me out since I couldn't find the answer anywhere in my searches.

Currently, I have a shiny app that just displays this bar chart from a dataset on Stock Returns for a Portfolio.

Created using this code:

portfolio = c(100,100,100,100)
my_portfolio <- data.table(EuStockMarkets)
my_portfolio$Date <- seq(as.Date('2011-01-01'),as.Date('2016-02-04'),by = 1)

my_portfolio$R_DAX <- NA
my_portfolio$R_SMI <- NA
my_portfolio$R_CAC <- NA
my_portfolio$R_FTSE <- NA
my_portfolio$Total_Return <- NA

for (i in 2:1860) {
  my_portfolio$R_DAX[i] <- portfolio[1]*(my_portfolio$DAX[i]-my_portfolio$DAX[i-1])
  my_portfolio$R_SMI[i] <- portfolio[2]*(my_portfolio$SMI[i]-my_portfolio$SMI[i-1])
  my_portfolio$R_CAC[i] <- portfolio[3]*(my_portfolio$CAC[i]-my_portfolio$CAC[i-1])
  my_portfolio$R_FTSE[i] <- portfolio[4]*(my_portfolio$FTSE[i]-my_portfolio$FTSE[i-1])
}

my_portfolio$Total_Return <- my_portfolio$R_DAX + my_portfolio$R_SMI + my_portfolio$R_CAC + my_portfolio$R_FTSE
plot_subset <- my_portfolio[2:100]

Bar chart of return

Now, what I'm looking for SEEMS simple to me, but after doing a lot of research, I'm getting advanced solutions that do not really fit my problem.

I would like this: When I click a bar on the plot (e.g the bar where return is lowest), I would like a table to display on the side displaying the date and stock prices of the stocks in the portfolio.

I've looked at these following resources, which provided me with great starting points regarding the click but not so much with the table updates:

https://plot.ly/r/shiny-coupled-events/

https://plot.ly/r/shiny-coupled-hover-events/#new-to-plotly

Retrieving source data information from a barplot in R shiny interactively

(This last link is the most helpful and it gives me the x and y coordinate of the click, but my biggest question is: how do I make the jump from that to table values?)

If plotly is not the best method here, what is an alternative?

Thank you for reading and helping with my problem. Any help is much appreciated!!

figNewton
  • 127
  • 1
  • 12

1 Answers1

0

Here is how I would solve this:

I removed that cumbersome for-loop, you should dig deeper into the wonderful library(data.table)

library(data.table)
library(plotly)
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(plotlyOutput("myPlot"),
                dataTableOutput("myTable"))
)

server <- function(input, output) {

  portfolio = c(100, 100, 100, 100)

  my_portfolio <- data.table(EuStockMarkets)
  my_portfolio[, Date := seq(as.Date('2011-01-01'), as.Date('2016-02-03'), by = 1)]
  my_portfolio[, R_DAX := portfolio[1] * c(NA, diff(DAX))]
  my_portfolio[, R_SMI := portfolio[2] * c(NA, diff(SMI))]
  my_portfolio[, R_CAC := portfolio[3] * c(NA, diff(CAC))]
  my_portfolio[, R_FTSE := portfolio[4] * c(NA, diff(FTSE))]
  my_portfolio[, Total_Return := R_DAX + R_SMI + R_CAC + R_FTSE]

  plot_subset <- my_portfolio[2:100]

  output$myPlot <- renderPlotly({
    plot_ly(
      plot_subset,
      source = "myClickSource",
      x =  ~ Date,
      y =  ~ Total_Return,
      type = "bar"
    )
  })

  SelectedBar <- reactiveVal(NULL)

  observe({
    myClicks <- event_data("plotly_click", source = "myClickSource")
    req(myClicks)
    print(myClicks)
    SelectedBar(as.Date(myClicks$x))
    })

  output$myTable <- renderDataTable({
      plot_subset[Date %in% SelectedBar()]
    })

}

shinyApp(ui, server)
SeGa
  • 9,454
  • 3
  • 31
  • 70
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • 1
    Thank you! I've only learned about the data.table library recently, and I'm seeing that it is much more powerful than I had initially seen. – figNewton Nov 29 '18 at 14:39