r - Shiny Plot Click Event - Updating Table Only
问题描述
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]
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!!
解决方案
以下是我将如何解决这个问题:
我删除了那个繁琐的for循环,你应该深入挖掘精彩的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)
推荐阅读
- jenkins - 使用 github webhook 将更改推送到特定分支时如何触发 jenkins 作业?
- java - Spring Boot Raw WebSocket 设置主体
- javascript - 使用 api 显示列表的 Angular 8 问题
- html - Google Drive 和翻译脚本
- google-apps-script - 是否可以动态更改 gmail 插件图标?
- java - 如何使用外部映射将 json 转换为 json
- sql - TSQL - 每天返回前 10 名客户
- javascript - AngularJS 日期过滤器添加了意外的时区偏移量?
- flutter - 在颤动中更改语言后如何刷新应用程序当前屏幕?
- html - 如何在 Vue js 中将相对图像 url 转换为绝对图像 url?