【发布时间】:2019-06-23 22:14:23
【问题描述】:
我有下面的数据框:
Name<-c("John","Bob","Jack")
Number<-c(3,3,5)
NN<-data.frame(Name,Number)
还有一个简单的闪亮应用程序,可以从中创建一个情节直方图。我的目标是单击直方图的一个条形图并在与该条形图对应的数据表中显示Name。例如,如果我单击第一个栏 3,我将获取一张包含 John 和 Bob 姓名的表格。
library(plotly)
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
plotlyOutput("heat")
),
DT::dataTableOutput('tbl4')
)
server <- function(input, output, session) {
output$heat <- renderPlotly({
p <- plot_ly(x = NN$Number, type = "histogram")
})
output$tbl4 <- renderDataTable({
s <- event_data("plotly_click")
if (length(s) == 0) {
"Click on a bar in the histogram to see its values"
} else {
NN[ which(NN$Number==as.numeric(s[2])), 1]
}
})
}
shinyApp(ui, server)
【问题讨论】:
-
如果再添加一列计数
2,2,1,这很容易。 Bcoz 当您单击绘图时,您将从直方图中获得计数,而不是实际值。你还好吗? -
我已经为你添加了一个解决方案。请让我知道你的看法。