闪亮的绘图点击事件 - 仅更新表 [英] Shiny Plot Click Event - Updating Table Only

查看:41
本文介绍了闪亮的绘图点击事件 - 仅更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这里有人可以帮助我,因为我在搜索中找不到任何答案.

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.

使用此代码创建:

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]

现在,我正在寻找的 SEEMS 对我来说很简单,但经过大量研究后,我得到了并不真正适合我的问题的高级解决方案.

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/闪亮耦合悬停事件/#new-to-plotly

从条形图中检索源数据信息在 R 中以交互方式闪亮

(最后一个链接最有用,它为我提供了点击的 x 和 y 坐标,但我最大的问题是:如何从该链接跳转到表格值?)

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

如果 plotly 不是这里最好的方法,还有什么替代方法?

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!!

推荐答案

我将如何解决这个问题:

Here is how I would solve this:

我删除了那个繁琐的 for 循环,你应该深入挖掘美妙的 library(data.table)

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)

这篇关于闪亮的绘图点击事件 - 仅更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆