【问题标题】:Get row number from of data point from event_data从 event_data 的数据点获取行号
【发布时间】:2016-05-30 16:51:29
【问题描述】:

我一直在使用闪亮和绘图创建数据查看器应用程序。我想为我的数据创建一个多维缩放视图,然后单击一个数据点以便能够将单个点查看为条形图。我受到this 示例的启发。

这是一个几乎可以工作的最小示例:

ui.r 文件

library(shiny)
library(mlbench)
library(plotly)
library(shinythemes)
library(dplyr)

# Load the data
allDat <- iris[,-5]

# ui.R definition
ui <- fluidPage(
  # Set theme
  theme = shinytheme("spacelab"),

  # Some help text
  h2("Inspect each element in iris data set"),
  h4("This a shiny app exploiting coupled events in plotly"),
  tags$ol(
    tags$li("The first chart showcases", tags$code("plotly_click"))
  ),

  # Vertical space
  tags$hr(),

  # First row
  fixedRow(
    column(6, plotlyOutput("Plot1", height = "600px")),
    column(6, plotlyOutput("Plot2", height = "600px"))),

  tags$hr())

server.r 文件

# server.R definition
server <- function(input, output){

  d <- dist(allDat) # euclidean distances between the rows
  fit <- cmdscale(d,eig=TRUE, k=2) # k is the number of dim

  # plot solution
  x <- fit$points[,1]
  y <- fit$points[,2]
  plot.df <- data.frame(x=x,y=y,allDat)

  output$Plot1 <- renderPlotly({
    plot_ly(plot.df, x = x, y = y, mode="markers", source = "mds") %>%
      layout(title = "MDS of iris data",
             plot_bgcolor = "6A446F")
  })

  # Coupled event 2
  output$Plot2 <- renderPlotly({

    # Try to get the mouse click data
    event.data <- event_data("plotly_click", source = "mds")

    # If NULL dont do anything
    if(is.null(event.data) == T) return(NULL)

    # I need the row of the data in the allDat data frame
    # pretty sure this is not correct
    ind <- as.numeric(event.data[2])

    p1 <- plot_ly(x = colnames(allDat), y=as.numeric(allDat[ind,]),type="bar")

  })

}

要运行它,将这两个文件放在一个名为 something 的文件夹中,例如dataViewer,然后从包含 dataViewer 文件夹的目录中运行runApp("dataViewer")

问题是什么,我在寻求什么?

我不明白event_data 函数的输出。我希望能够单击散点图上的一个点并从allDat 数据帧或plot.df 数据帧中提取该数据点的行号,因为它应该是相同的。然后我想使用该行号来进一步可视化右侧条形图中的特定点。

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    我查看了event.data 对象,认为您要查找的值是event.data$pointNumber(以0 开头,因此您需要使用event.data$pointNumber + 1 来识别行)。

    event.data 是一个包含四个名称的列表:curveNumberpointNumberxy

    【讨论】:

    • 感谢您的回复!我在看here,对此有评论:“pointNumber:正在绘制的数据点的索引。请注意,这与curveNumber相关联并从0开始”。所以这从 0 开始,并以某种方式链接到 curveNumber,我不知道它是什么。那么这是用于绘图的数据框中的索引,还是其他一些索引?
    • 好的,干杯!希望这将帮助其他可能遇到此问题的人!我是新来的情节和闪亮的,但如果我可能会问,你是如何检查 event.data 对象的?
    • 编辑:我一定是记错了,但pointNumber似乎是从0开始的,所以你应该使用event.data$pointNumber + 1来识别行。
    • 您可以使用shiny.rstudio.com/articles/debugging.html 来调试您的应用程序,虽然我只是添加了一个 textOutput 来显示对象和名称的摘要
    • 仅供参考,不建议使用pointNumber查找行。请看这个answer
    猜你喜欢
    • 2021-08-19
    • 2020-03-01
    • 2011-06-22
    • 2011-05-29
    • 1970-01-01
    • 2021-10-18
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多