r - 显示模态 onclick 条形图

标签 r shiny plotly

当我点击绘图的条形图时,我想要一个弹出窗口。

library(shiny)
library(shinymaterial)
df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
y = c(rnorm(10), rnorm(10, 3, 1)))
ui <- material_page(title = "Material Design",
  tags$br(),
  font_color = "cyan darken-5",
  nav_bar_color = "cyan darken-5",
  plotlyOutput('scatter')
)
server <- function(input, output) {
  output$scatter <- renderPlotly({
    plot_ly(df1, x = df1$x, y = df1$y, type = 'bar', source = 'scatter')
  })
}
shinyApp(ui = ui, server = server)

我突然意识到我需要帮助;我想要点击时弹出窗口 条形图上显示相应的内容。 请帮助我。

最佳答案

您可以使用观察者和散点图中的 event_data 来完成此任务。下面是工作示例,希望对您有所帮助!

library(shiny)
library(plotly)
df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
                  y = c(rnorm(10), rnorm(10, 3, 1)))
ui <- fluidPage(
                    plotlyOutput('scatter')
)
server <- function(input, output) {
  output$scatter <- renderPlotly({
    plot_ly(df1, x = df1$x, y = df1$y, type = 'bar', source = 'scatter')
  })

  observeEvent(event_data("plotly_click", source = "scatter"),
               {
                 event_data = event_data("plotly_click", source = "scatter")
                 showModal(modalDialog(
                   title = "Important message",
                   paste0('x: ', event_data$x, ', y: ', event_data$y)
                 ))
               }
               )
}
shinyApp(ui = ui, server = server)

关于r - 显示模态 onclick 条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369827/

相关文章:

r - 使用 mapply 将 lm 模型列表拟合到数据框列表 [R]

r - 在 R 中对连续日期进行分组

r - 将下拉列表添加到 DT 表中的每一列,其中下拉列表中的值是从另一个数据帧获取的

r - 绑定(bind)/取消绑定(bind) DataTable 时出现 react 问题

python - 使用 Dash/Plotly 绘制分类散点图

python - 在 Jupyter Notebook 中保存 Plotly 离线图像?

r - 排除列名包含特定字符的列,并删除剩余列中包含 NA 的行

r - 将带有美元符号 ($) 的变量与 facet_grid() 或 facet_wrap() 组合传递给 aes() 时出现问题

checkbox - Shiny - 通过 id 访问各个复选框项目

python - 您将如何在 Python 中绘制此 3D 可视化图?