【问题标题】:Is there a way to open a dataset when I click on a section in pie chart?当我单击饼图中的某个部分时,有没有办法打开数据集?
【发布时间】:2021-11-25 10:34:15
【问题描述】:

我想创建一个饼图,当我单击某个部分时,我应该会显示一个数据框。 例如,我可能会创建以下饼图:

# Create data for the graph.
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")

# Give the chart file a name.
png(file = "city.png")

# Plot the chart.
pie(x,labels)

现在让我们说当我点击诸如“伦敦”之类的切片时,我得到了 IRIS 数据集。

我使用的解决方案:

library(shiny)
library(plotly)

df <- https://drive.google.com/file/d/1RT5AkCef4cehEaGelK0avbXAtck1f-Ap/view   #READ THIS DATA HERE
setDT(df)
dtnum <- df[ , .N, by="V3"]
dtnum2 <- df[ , .N, by="V2"]
ui <- fluidPage(
  
  plotlyOutput("myPlot"),
  plotlyOutput("myPlot2"),
  DTOutput("mydt")
  
)

server <- function(input, output, session) {
  
  observe({
    d <- event_data("plotly_click")
    print(d)
    if (is.null(d)) {
      df
    } else {
      output$mydt <- renderDT({
        df[V3 == d$customdata]
      })
    }
  })
  output$myPlot2 <- renderPlotly({
    plot_ly(dtnum2, labels = ~V2, values = ~N, type = 'pie', customdata = ~V2)
  }) 
  
  output$myPlot <- renderPlotly({
    plot_ly(dtnum, labels = ~V3, values = ~N, type = 'pie', customdata = ~V3)
  })
  
}

shinyApp(ui, server)

【问题讨论】:

  • pie() 会给你一张照片。因此,我们需要其他东西来构建饼图。也许“highcharter”是你想要的。有几个主题。如果你想把它连接到一张桌子上,我认为你需要使用闪亮的。祝你好运。我渴望看到结果。听起来是个好主意!
  • 在我的编辑中查看我的解决方案

标签: r


【解决方案1】:

这是一个使用shinyplotlyDT 的示例。

要了解发生了什么,请查看linking views with shinysupplying custom data 上的情节书。

library(data.table)
library(plotly)
library(DT)
library(datasets)
library(shiny)

irisDT <- copy(iris)
setDT(irisDT)

ui <- fluidPage(
  plotlyOutput("myPlot"),
  DT::dataTableOutput("myTable")
)

server <- function(input, output, session) {
  output$myPlot <- renderPlotly({
    irisCountDT <- irisDT[,.N, by="Species"]
    fig <- plot_ly(irisCountDT, labels = ~Species, values = ~N, type = 'pie', source = "myPlotSource", customdata = ~Species)
  })
  
  myPlotEventData <- reactive({
    event_data(
      event = "plotly_click",
      source = "myPlotSource")
    })
  
  output$myTable <- DT::renderDataTable({
    datatable(irisDT[Species %in% myPlotEventData()$customdata[[1]]])
  })
}

shinyApp(ui, server)

在这种情况下,还要检查 plotly 的 capabilities regarding crosstalk

【讨论】:

  • 令人兴奋!让我花点时间了解一下。我认为这是不可能的,我应该放弃
  • source="myPlotSource" 是做什么的?我很难理解这个
  • 它将情节和 event_data 反应式联系起来。当有多个绘图要接收事件时,这一点很重要,因此您可以定义去哪里。
  • 我的眼睛应该进入一个标记为 [闪亮] 的单独问题 - 请使用 dput(df) 的输出而不是谷歌驱动器来共享您的数据。
  • 我在这里发布了一个新问题stackoverflow.com/questions/70113381/…
猜你喜欢
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2016-11-18
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多