R 从事件_数据获取数据点的行数
我一直在使用shiny和plotly创建data viewer应用程序。我想创建一个数据的多维缩放视图,然后单击一个数据点,以便能够以条形图的形式查看单个点。我受到了榜样的启发 下面是一个几乎可以正常工作的示例: ui.r文件R 从事件_数据获取数据点的行数,r,shiny,plotly,R,Shiny,Plotly,我一直在使用shiny和plotly创建data viewer应用程序。我想创建一个数据的多维缩放视图,然后单击一个数据点,以便能够以条形图的形式查看单个点。我受到了榜样的启发 下面是一个几乎可以正常工作的示例: ui.r文件 库(闪亮) 图书馆(mlbench) 图书馆(绘本) 图书馆(shinythemes) 图书馆(dplyr) #加载数据 allDat我查看了event.data对象,认为您要查找的值是event.data$pointNumber(它以0开头,因此需要使用event.da
库(闪亮)
图书馆(mlbench)
图书馆(绘本)
图书馆(shinythemes)
图书馆(dplyr)
#加载数据
allDat我查看了event.data
对象,认为您要查找的值是event.data$pointNumber
(它以0开头,因此需要使用event.data$pointNumber+1
来标识行)
event.data
是一个包含四个名称的列表:curveNumber
、pointNumber
、x
和y
感谢您的回复!我一直在看,这里有一条评论:“pointNumber:图表中数据点的索引。请注意,它链接到CurveEnumber,从0开始”。所以它从0开始,以某种方式链接到curveNumber
,我不知道它是什么。那么这是用于绘图的数据框中的索引,还是其他索引?好的,干杯!希望这将帮助其他可能遇到这种情况的人!我不熟悉plotly和Shinny,但如果我可能会问,您是如何检查event.data对象的?编辑:我一定是计算错误,但似乎pointNumber
以0开头,因此您应该使用event.data$pointNumber+1
来标识行。您可以使用,虽然我只是添加了一个textOutput来显示对象和名称的摘要,仅供他人参考,但不建议使用pointNumber
查找行。请看这个
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 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")
})
}