从 event_data 中获取数据点的行号
Get row number from of data point from event_data
我一直在使用 shiny 和 plotly 创建一个数据查看器应用程序。我想为我的数据创建一个多维缩放视图,然后单击一个数据点以能够将单个点作为条形图查看。 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
数据框中提取该数据点的行号,因为它应该是相同的。然后我想使用该行号进一步可视化右侧条形图中的特定点。
我查看了 event.data
对象,认为您要查找的值是 event.data$pointNumber
(以 0 开头,因此您需要使用 event.data$pointNumber + 1
来标识该行) .
event.data
是一个包含四个名称的列表:curveNumber
、pointNumber
、x
和 y
.
我一直在使用 shiny 和 plotly 创建一个数据查看器应用程序。我想为我的数据创建一个多维缩放视图,然后单击一个数据点以能够将单个点作为条形图查看。 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
数据框中提取该数据点的行号,因为它应该是相同的。然后我想使用该行号进一步可视化右侧条形图中的特定点。
我查看了 event.data
对象,认为您要查找的值是 event.data$pointNumber
(以 0 开头,因此您需要使用 event.data$pointNumber + 1
来标识该行) .
event.data
是一个包含四个名称的列表:curveNumber
、pointNumber
、x
和 y
.