3

I want to create a pie chart and when I click on a section I should get a dataframe displayed. For example I might create the following pie chart:

# 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)

enter image description here

Now lets say when I click on a slice such as 'london' I get the IRIS datset.

Solution i used:

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)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
ujjwal tyagi
  • 493
  • 2
  • 8
  • `pie()` will get you a picture. Therefore we need something else to build the piechart. Maybe "highcharter" is the one you seek. There are a few topics on that. If you want to connect it to a table, I think you will need to use shiny. good luck. I am eager to see a result. sound like a good idea! – Lucas Nov 25 '21 at 10:45
  • Check out my solution in my edit – ujjwal tyagi Nov 25 '21 at 14:55

1 Answers1

4

Here is an example using shiny, plotly and DT.

To understand what's going on please check the plotly book on linking views with shiny and supplying 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)

result

Also check plotly's capabilities regarding crosstalk in this context.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • 2
    Mindblowing! Let me spend some time to get my head around it. I thought this wouldnt be possible and i should give up – ujjwal tyagi Nov 25 '21 at 11:26
  • What does source = "myPlotSource" do? I am having a little trouble understanding this one – ujjwal tyagi Nov 25 '21 at 12:38
  • 2
    It links the plot and the event_data reactive. This is important when having multiple plots to receive events from, so you can define what goes where. – ismirsehregal Nov 25 '21 at 12:46
  • Thank you so much. I was finally able to do it on my data too. https://i.imgur.com/LFOfLF9.png – ujjwal tyagi Nov 25 '21 at 14:52
  • I have posted my solution based on your code in my edit. Also i added multiple pie charts now. Would it be possible to show data from multiple pie charts into the same table? – ujjwal tyagi Nov 25 '21 at 14:55
  • 2
    I my eyes that should go into a separate question tagged [shiny] - please use the output of `dput(df)` instead of google drive to share your data. – ismirsehregal Nov 25 '21 at 14:57
  • 1
    I have posted a new question here https://stackoverflow.com/questions/70113381/create-a-data-table-when-click-on-section-of-different-pie-charts – ujjwal tyagi Nov 25 '21 at 15:06
  • I share your view ujjwal, ismirsehregal turns the impossible into the possible! – Curious Jorge - user9788072 Mar 23 '22 at 16:02
  • Thanks @Curious Jorge-user9788072! Just FYI all those upvotes were removed again (from my internal score). SO automatically detects and reverses serial voting. – ismirsehregal Mar 25 '22 at 05:58
  • Wow!! That's too bad, your posts are always tops and I've been upvoting those posts more related to what I'm working on. Nothing random. This OP is a perfect example where you access a dataset by clicking on a chart, it'll be very useful for my projects. Oh well, live and learn. – Curious Jorge - user9788072 Mar 25 '22 at 13:16
  • Is anyone having trouble achieving this with a plotly histogram? Where you each bar is an interval – Danish Zahid Malik Apr 04 '23 at 13:29