如何获取 Rplotly 的旭日图中点击切片的 event_data

2023-12-15

当使用plotly R包在Shiny应用程序中创建旭日饼图时,用户可以单击图来动态放大/缩小。我们希望能够下载当前选定/居中片段的一些数据。

但是,我们无法从所有可能的 eventdata 选项中找到此信息。有悬停事件,但这还不够,因为用户可以单击一个片段,然后鼠标移动以悬停在其他片段上,而无需单击它。

没有放大/缩小的点击事件。并且没有重新布局事件。我认为一定有一些 js 事件是通过放大/缩小触发的,但现有的 eventdata 函数没有捕获该事件。

更新: 好像有selectedPathjs 图表的属性,但是我不知道如何在 Shiny 中访问这些数据。

Update2:感谢您的回答解决了问题。而且它被证明是plotly R 包中缺少的功能,并且它已在最近的提交中使用.


现在plotly_click仅提供旭日图的根和叶的数据。 但是您可以通过plotly_hover event_data to a reactiveVal一旦情节被点击。

由于您没有提供任何示例,请参阅以下内容 - 使用onclick() from library(shinyjs)解决方法。

library(plotly)
library(shinyjs)

DF <- structure(list(labels = c("total", "A", "C", "B", "F", "E", "F", 
                                "E", "D", "E", "D", "D", "F", "G", "H", "H", "G", "H", "G", "H", 
                                "H", "G", "I", "G", "I", "H", "G", "I", "I", "G", "G", "I", "H", 
                                "H", "I", "I", "I", "H", "I", "G"),
                     values = c(100L, 36L, 29L,
                                35L, 12L, 14L, 10L, 14L, 8L, 18L, 5L, 10L, 9L, 6L, 5L, 4L, 3L,
                                7L, 4L, 2L, 6L, 3L, 8L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 5L, 
                                5L, 2L, 2L, 1L, 1L, 1L, 5L),
                     parents = c(NA, "total", "total", "total", "total - A", "total - C", "total - C", "total - A",
                                 "total - B", "total - B", "total - C", "total - A", "total - B",
                                 "total - A - F", "total - C - E", "total - C - F", "total - A - E",
                                 "total - A - E", "total - B - D", "total - B - D", "total - B - E",
                                 "total - C - D", "total - B - E", "total - A - D", "total - B - D",
                                 "total - B - F", "total - C - F", "total - A - E", "total - C - E",
                                 "total - B - E", "total - B - F", "total - A - D", "total - A - D",
                                 "total - A - F", "total - B - F", "total - C - F", "total - C - D",
                                 "total - C - D", "total - A - F", "total - C - E"),
                     ids = c(
                       "total", "total - A", "total - C", "total - B", "total - A - F", "total - C - E", 
                       "total - C - F", "total - A - E", "total - B - D", "total - B - E", "total - C - D",
                       "total - A - D", "total - B - F", "total - A - F - G", "total - C - E - H",
                       "total - C - F - H", "total - A - E - G", "total - A - E - H", "total - B - D - G",
                       "total - B - D - H", "total - B - E - H", "total - C - D - G", "total - B - E - I",
                       "total - A - D - G", "total - B - D - I", "total - B - F - H", "total - C - F - G",
                       "total - A - E - I", "total - C - E - I", "total - B - E - G", "total - B - F - G",
                       "total - A - D - I", "total - A - D - H", "total - A - F - H", "total - B - F - I",
                       "total - C - F - I", "total - C - D - I", "total - C - D - H", "total - A - F - I",
                       "total - C - E - G"
                     )), row.names = c(NA,-40L), class = "data.frame")

ui <- fluidPage(
  useShinyjs(),
  plotlyOutput("sunburst"),
  htmlOutput("hoverDataOut"),
  htmlOutput("clickDataOut")
)

server <- function(input, output, session) {
  output$sunburst <- renderPlotly({
    plot_ly(data = DF, source = "sunSource", customdata = ~ids, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')
  })

  hoverData <- reactive({
    currentEventData <- unlist(event_data(event = "plotly_hover", source = "sunSource", priority = "event"))
  })

  clickData <- reactiveVal()

  observe({
    clickData(unlist(event_data(event = "plotly_click", source = "sunSource", priority = "event")))
  })

  onclick(id = "sunburst", expr = {clickData(hoverData())})

  output$hoverDataOut <- renderText({
    paste("Hover data:", paste(names(hoverData()), unlist(hoverData()), sep = ": ", collapse = " | "))
  })

  output$clickDataOut <- renderText({
    paste("Click data:", paste(names(clickData()), unlist(clickData()), sep = ": ", collapse = " | "))
  })

}

shinyApp(ui, server)

Result


我创建了一个 GitHubissue获取有关此行为的更多信息。

你也可能对此有兴趣这个帖子.


Update:最新提交后的反应this plotly_sunburstclick可以使用如下:

# install latest r-plotly dev version:
# devtools::install_github("ropensci/plotly")

library(plotly)
library(shinyjs)

DF <- structure(list(labels = c("total", "A", "C", "B", "F", "E", "F", 
                                "E", "D", "E", "D", "D", "F", "G", "H", "H", "G", "H", "G", "H", 
                                "H", "G", "I", "G", "I", "H", "G", "I", "I", "G", "G", "I", "H", 
                                "H", "I", "I", "I", "H", "I", "G"),
                     values = c(100L, 36L, 29L,
                                35L, 12L, 14L, 10L, 14L, 8L, 18L, 5L, 10L, 9L, 6L, 5L, 4L, 3L,
                                7L, 4L, 2L, 6L, 3L, 8L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 5L, 
                                5L, 2L, 2L, 1L, 1L, 1L, 5L),
                     parents = c(NA, "total", "total", "total", "total - A", "total - C", "total - C", "total - A",
                                 "total - B", "total - B", "total - C", "total - A", "total - B",
                                 "total - A - F", "total - C - E", "total - C - F", "total - A - E",
                                 "total - A - E", "total - B - D", "total - B - D", "total - B - E",
                                 "total - C - D", "total - B - E", "total - A - D", "total - B - D",
                                 "total - B - F", "total - C - F", "total - A - E", "total - C - E",
                                 "total - B - E", "total - B - F", "total - A - D", "total - A - D",
                                 "total - A - F", "total - B - F", "total - C - F", "total - C - D",
                                 "total - C - D", "total - A - F", "total - C - E"),
                     ids = c(
                       "total", "total - A", "total - C", "total - B", "total - A - F", "total - C - E", 
                       "total - C - F", "total - A - E", "total - B - D", "total - B - E", "total - C - D",
                       "total - A - D", "total - B - F", "total - A - F - G", "total - C - E - H",
                       "total - C - F - H", "total - A - E - G", "total - A - E - H", "total - B - D - G",
                       "total - B - D - H", "total - B - E - H", "total - C - D - G", "total - B - E - I",
                       "total - A - D - G", "total - B - D - I", "total - B - F - H", "total - C - F - G",
                       "total - A - E - I", "total - C - E - I", "total - B - E - G", "total - B - F - G",
                       "total - A - D - I", "total - A - D - H", "total - A - F - H", "total - B - F - I",
                       "total - C - F - I", "total - C - D - I", "total - C - D - H", "total - A - F - I",
                       "total - C - E - G"
                     )), row.names = c(NA,-40L), class = "data.frame")

ui <- fluidPage(
  useShinyjs(),
  plotlyOutput("sunburst"),
  htmlOutput("hoverDataOut"),
  htmlOutput("clickDataOut")
)

server <- function(input, output, session) {
  output$sunburst <- renderPlotly({
    plot_ly(data = DF, source = "sunSource", customdata = ~ids, ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, type='sunburst', branchvalues = 'total')
  })

  hoverData <- reactive({
    currentEventData <- unlist(event_data(event = "plotly_hover", source = "sunSource", priority = "event"))
  })

  clickData <- reactive({
    currentEventData <- unlist(event_data(event = "plotly_sunburstclick", source = "sunSource", priority = "event"))
  })

  output$hoverDataOut <- renderText({
    paste("Hover data:", paste(names(hoverData()), unlist(hoverData()), sep = ": ", collapse = " | "))
  })

  output$clickDataOut <- renderText({
    paste("Click data:", paste(names(clickData()), unlist(clickData()), sep = ": ", collapse = " | "))
  })

}

shinyApp(ui, server)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取 Rplotly 的旭日图中点击切片的 event_data 的相关文章

  • 无需重新绘制传单地图即可进行闪亮的 UI 调整

    问题 我正在创建一个闪亮的仪表板来帮助客户探索一些空间数据 我想要实现的 UI 设计允许用户轻松地在两种布局之间切换 Map Only 地图 数据表 我在实现这种设计时遇到了麻烦 因为每次用户在布局之间切换时都会出现两个问题 地图已重新绘制
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • 使用操作按钮在闪亮的 R 中添加包含现有数据框的新行

    我正在构建一个闪亮的表单 它将从 textInput 字段获取数据 并将这些输入与文本文件 将通过文件输入上传 组合起来 并在主面板中显示输出 有一个操作按钮用于第一次更新数据 从文本输入中获取数据并与处理后的文本文件合并 我添加了另一个操
  • 如何修改反应链以便最后修改的对象控制其他链接的对象?

    新注释 1 最终解决的代码发布在最底部 反映了 ismirsehregal 于 2021 年 12 月 3 日的解决方案 以及一些标记为 ADDED 和 MODIFIED 的小调整 ADD 是为了解决我在矩阵 2 添加值后从矩阵 1 中删除
  • 在模块中使用shiny的renderUI

    这是我在 stackoverflow 上的第一个问题 我在闪亮 1 0 5 中遇到模块和 renderUI 的问题 当我在中使用 renderUI 时 Main Part ui lt bootstrapPage uiOutput Dynam
  • Shiny App:如何动态更改server.R中的框标题?

    框标题通常在 ui R 中设置 是否可以在 server R 中动态更改框标题 ui R box title Dynamic title here plotOutput barPlot server R output barPlot ren
  • 使用Shiny和Shinydashboard时如何使图标大小一致?

    我在闪亮的应用程序中添加可点击的图标以显示弹出信息框 请参阅以下屏幕截图和代码示例 我的策略是将我的文本和代码包装起来actionLink in the HTML功能 这效果很好 然而 图标的大小是由关联的大小决定的 我想知道是否可以使所有
  • Shiny 中的模态对话框:可以调整宽度但不能调整高度

    在我的 Shiny 应用程序中 我有几个来自闪亮BS 包的模式窗口 我可以像这样调整这些模式窗口的宽度 tags head tags style HTML modal lg width 1200px abs 1 background col
  • 在 Shiny 中使用 readlines(prompt = )

    我有一个代码 使用以下方式获取输入readlines prompt 功能 您能告诉我 Shiny 中的哪个输入函数足以将此代码适应 Shiny 应用程序吗 我需要一个交互功能 我无法使用简单的输入selectInput 因为我有很多read
  • r Shiny 中的 fileInput 函数没有响应

    我是 R 和 R闪亮的新手 一直致力于构建一个统计应用程序 该应用程序将允许用户导入文件 然后对数据运行不同的统计程序 直到最近 fileData 函数一直对我来说运行良好 现在每当我尝试上传文件时 都不会打开任何内容 我已尝试了所有我能想
  • R:如何更改ggvis闪亮应用程序中特定范围的绘图背景颜色

    I have a simple shiny app like below and you can run it The plots are created by ggvis and user can choose student name
  • 如何将 Shiny 中生成的反应图传递到 Rmarkdown 以生成动态报告

    简而言之 我希望能够通过单击按钮从我的闪亮应用程序生成动态 Rmarkdown 报告文件 pdf 或 html 为此 我想我将使用 Shiny 的参数化报告 但不知何故 我无法将单个谜题转移到所需的目标 使用此代码 我们可以在 R Shin
  • R Shiny 中表格的条件格式

    我正在尝试可视化队列分析 并想使用RenderDataTable闪亮以获得这种可视化效果 我将能够突出显示基于具有值 1 0 的单独列的所有单元格 其中 1 被着色 0 不被着色 我尝试了几件事 包括尝试使用geom tile in ggp
  • 带有 R 传单的自定义图例 - 同一图例中的圆形和正方形

    我想创建一个传单地图 在同一个传单图图例中既有圆形又有正方形 到目前为止 我已经使用了上一篇文章中的建议 并在我闪亮的 UI 代码中添加了以下代码 tags style type text css html body width 100 h
  • 具有不同侧边栏的选项卡

    我正在尝试创建一个带有多个选项卡的闪亮应用程序 每个选项卡都有自己的侧边栏 我还没能让它发挥作用 任何有关错误的帮助将不胜感激 下面是代码 ui lt fluidPage titlePanel Hi sidebarLayout positi
  • 使用 R Shiny 从 XLConnect 下载 Excel 文件

    有没有人尝试过使用 R Shiny 中的下载处理程序通过 XLConnect 下载新创建的 Excel 文件 在 ui R 中有一行不起眼的行 downloadButton downloadData Download 在 server R
  • 将 Instagram/youtube 嵌入 Shiny R 应用程序

    我想通过点击图表来播放 Instagram 或 Youtube 视频 例如显示异常值等 到目前为止 明确告诉 Shiny 视频内容是有效的 require shiny require ggplot2 data df lt data fram
  • R闪亮数据表在开始时不显示记录(行)

    我正在构建一个带有数据表的闪亮应用程序 我想要的是启动时不显示任何记录 行 这样您只能看到表格顶部的过滤器 当您开始输入时 会显示行 我在数据表中找不到选项 这可能吗 下面是示例代码 shinyApp ui navbarPage title
  • 在 Shiny 中设置一个绘图缩放以匹配另一个绘图缩放

    我正在尝试使用情节重排获取一个图的 x 轴缩放限制 并将它们应用到 Shiny 中的另一个图 到目前为止 我可以从 plot1 x轴限制 获取相关的plotly relayout数据 将其转换 从数字到日期 并在绘制 plot2 之前将其提
  • 单击 R 中的 Sankey Chart 线时添加额外的标签值

    以下 R 闪亮脚本创建一个桑基图 如下面的快照所示 我的要求是 当我单击左右节点之间的任何链接 即 a1 和 a2 时 我希望相应的 a3 的总和出现在标签中 例如 a1 中的 A 和 a2 中的 E 总共具有值 50 和 32 因此 我想

随机推荐

  • 这是 GAC 的所在地吗?

    这是 GAC 的所在地吗 C Windows Microsoft NET assembly GAC MSIL 如果是这样 那么什么是C Windows assembly 答案取决于所使用的 NET Framework 的版本 多个版本可以并
  • 将设备指针传递给 cublasSnrm2 时出现分段错误

    下面的 cublas 代码给了我们错误 core dumped while being at cublasSnrm2 handle row dy incy de 你能给一些建议吗 main cu include
  • 如何从 MySQL 表中获取所有数据组合? [复制]

    这个问题在这里已经有答案了 在过去的五个小时里 我一直试图从表格中获取每种产品选项组合 但现在我完全陷入困境 我有一个表 其中包含如下数据 简化 CREATE TABLE assigned options option id int 10
  • 如何将 C 文件编译为可执行 (.exe) 文件?

    我不确定在 Windows 7 下使用 Cygwin 将 C 文件编译为可执行文件 谁能告诉我该怎么做吗 我读过一些教程但仍然不明白 我知道我需要一个Makefile 但是我应该在其中写入什么才能在编译过程后获得可执行文件呢 一开始我会说安
  • Spring:无法将模拟注入到使用 @Aspect 注释的类中

    我使用 AspectJ 创建了一个 Before 建议 package test accesscontrol permissionchecker import test accesscontrol database SessionExpir
  • 编写 JUnit 测试

    拜托 我是Java新手 如何为以下程序编写 JUnit 测试 要测试的程序 package codekeeper author henryjoseph import java util import java io public class
  • Conda 仅为 env 设置 LD_LIBRARY_PATH [重复]

    这个问题在这里已经有答案了 我安装了 miniconda3 其中创建了一个名为 py35 的虚拟环境 我有一些只想在这个环境中使用的库 因此他们处于 miniconda3 envs py35 libs 但是 由于 LD LIBRARY PA
  • Java JsonObject 数组值到键

    我是java新手 所以这有点令人困惑 我想获取json格式的字符串 我想要的结果是 user name lamis 我目前正在做的是这样的 JSONObject json new JSONObject json put name Lamis
  • .net 位图改变图像的颜色

    我正在尝试调整图像大小而不丢失任何颜色 但我无法让它发挥作用 看下图 上一张是原图 下一张是经过 NET的 我的问题是 如何保留颜色 我尝试了很多不同的设置 寻找 GDI 错误 更改调色板 将 ImageAttributes 更改为人们在互
  • 聚合与组合

    我想知道之间的关系是Human and a Driver License聚合还是组合 我很清楚Room and a Building关系是一个组合和一个Chair and a Room是一个聚合 但一个Driver License可以在没有
  • Visual Studio 2022 - v.17.6.5 - 在 _Layout.cshtml 中链接安装的 nuget 包 css 文件

    我创建了一个新的 net 6 0 MVC Web project并删除了预装的bootstrap from wwwroot lib因为我想添加它NuGet Package Manager 安装软件包后 文件将显示在我的中作为参考wwwroo
  • 在多个控制器操作中保留值

    在我的控制器中 我有一个数据库中对象的 ID 并且多个操作需要此 ID 来访问该对象 该 ID 是在Index操作并用于填充视图模型 我获取 ID 的方式是通过一个参数 EPiServer CMS 外 将Index行动 所以我无法在其他地方
  • 获取文件夹中的所有音频文件

    如何使用 ManagedQuery 从 Android 中的特定文件夹中获取所有音频文件 表示 ManagedQuery 中的 where 子句应该是什么来过滤 Cursor 结果 Code is String proj MediaStor
  • 使用 PHPmailer 发送多封电子邮件

    编辑 我忘记了我创建了SendMail 函数我自己 这就是为什么解释一开始没有提到它的作用 我在使用 PHPMailer 时遇到了一些问题 https github com PHPMailer PHPMailer 当尝试发送两封电子邮件时
  • 使用 Ruby 在多个操作系统中读取文件名而不会出现编码问题

    我想获取 UTF 8 格式的所有文件名 例如 当我在 Windows 中读取文件名后 我会 filename Iconv iconv UTF 8 Windows 1251 filename 在 Ubuntu 中 我不转换文件名并以 UTF
  • 如何制作类似于 Dropbox 的登录按钮?

    我正在尝试做类似 Dropbox 的登录按钮 有一个线程jQuery 中的 dropbox 登录弹出方法 但我对此无能为力 我希望当我按下登录按钮时将其打开 与 dropbox com 相同 这是示例代码 现在它可以在悬停时运行 但我想要点
  • ASP.NET 自定义控件

    如何创建自定义控件 不是 ASCX 控件 更重要的是 如何在项目中使用它 我不想为它创建一个单独的项目或将其编译为 DLL 服务器控件应编译成 DLL 没有理由害怕在项目中添加额外的程序集 它有助于创建良好的项目组织 ASP NET 服务器
  • jsf2.0 - 如何获取请求范围内其他 jsf 页面 bean 中的值

    我有两个页面 myaccount xhtml 和 selectbank xhtml 在我的帐户页面中 有一个充值帐户选项 当用户按下提交按钮时 用户将在其中输入金额 然后它将使用以下 bean 方法转到选择银行页面 public Strin
  • 更改 mat-list-option 中的文本颜色

    我在用mat selection list组件其中mat list option正在显示一个contact list如下所示 Now the background color当我点击特定时正在改变contact name 例如格雷姆天鹅 和
  • 如何获取 Rplotly 的旭日图中点击切片的 event_data

    当使用plotly R包在Shiny应用程序中创建旭日饼图时 用户可以单击图来动态放大 缩小 我们希望能够下载当前选定 居中片段的一些数据 但是 我们无法从所有可能的 eventdata 选项中找到此信息 有悬停事件 但这还不够 因为用户可