The following app opens up a dataframe which displays data related to the section of pie chart on which you click. This app works only on one pie chart at the moment but i want it to work on multiple pie charts. Is there any way to do it?
library(shiny)
library(plotly)
df <- structure(list(V1 = c("PM00FQ01", "PM00GQ01", "PM00KQ02", "PM033Q01", "PM034Q01T", "PM155Q01", "PM155Q02D", "PM155Q03D", "PM155Q04T", "PM192Q01T", "PM273Q01T", "PM305Q01", "PM406Q01", "PM406Q02", "PM408Q01T", "PM411Q01", "PM411Q02", "PM420Q01T", "PM423Q01", "PM442Q02", "PM446Q01", "PM446Q02", "PM447Q01", "PM462Q01D", "PM464Q01T", "PM474Q01", "PM496Q01T", "PM496Q02", "PM559Q01", "PM564Q01", "PM564Q02", "PM571Q01", "PM603Q01T", "PM800Q01", "PM803Q01T", "PM828Q01", "PM828Q02", "PM828Q03", "PM903Q01", "PM903Q03", "PM905Q01T", "PM905Q02", "PM906Q01", "PM906Q02", "PM909Q01", "PM909Q02", "PM909Q03", "PM915Q01", "PM915Q02", "PM918Q01", "PM918Q02", "PM918Q05", "PM919Q01", "PM919Q02", "PM923Q01", "PM923Q03", "PM923Q04", "PM924Q02", "PM934Q01", "PM934Q02", "PM936Q01", "PM936Q02", "PM939Q01", "PM939Q02", "PM942Q01", "PM942Q02", "PM942Q03", "PM943Q01", "PM943Q02", "PM948Q01", "PM948Q02", "PM948Q03", "PM949Q01T", "PM949Q02T", "PM949Q03", "PM953Q02", "PM953Q03", "PM953Q04D", "PM954Q01", "PM954Q02", "PM954Q04", "PM955Q01", "PM955Q02", "PM955Q03", "PM957Q01", "PM957Q02", "PM957Q03", "PM961Q02", "PM961Q03", "PM961Q05", "PM967Q01", "PM967Q03T", "PM982Q01", "PM982Q02", "PM982Q03T", "PM982Q04", "PM985Q01", "PM985Q02", "PM985Q03", "PM991Q01", "PM991Q02D", "PM992Q01", "PM992Q02", "PM992Q03", "PM995Q01", "PM995Q02", "PM995Q03", "PM998Q02", "PM998Q04T"), V2 = c("space and shape", "space and shape", "space and shape", "space and shape", "space and shape", "change and relationships", "change and relationships", "change and relationships", "change and relationships", "change and relationships", "space and shape", "space and shape", "space and shape", "space and shape", "uncertainty and data", "quantity", "uncertainty and data", "uncertainty and data", "uncertainty and data", "quantity", "change and relationships", "change and relationships", "space and shape", "space and shape", "space and shape", "quantity", "quantity", "quantity", "quantity", "quantity", "uncertainty and data", "change and relationships", "quantity", "quantity", "uncertainty and data", "change and relationships", "uncertainty and data", "quantity", "change and relationships", "change and relationships", "quantity", "quantity", "quantity", "quantity", "quantity", "quantity", "change and relationships", "uncertainty and data", "change and relationships", "uncertainty and data", "uncertainty and data", "uncertainty and data", "quantity", "quantity", "quantity", "space and shape", "change and relationships", "quantity", "space and shape", "space and shape", "change and relationships", "change and relationships", "uncertainty and data", "uncertainty and data", "quantity", "change and relationships", "quantity", "change and relationships", "space and shape", "quantity", "quantity", "quantity", "space and shape", "space and shape", "space and shape", "uncertainty and data", "uncertainty and data", "uncertainty and data", "change and relationships", "change and relationships", "change and relationships", "uncertainty and data", "uncertainty and data", "uncertainty and data", "change and relationships", "change and relationships", "change and relationships", "change and relationships", "change and relationships", "uncertainty and data", "space and shape", "space and shape", "uncertainty and data", "uncertainty and data", "uncertainty and data", "uncertainty and data", "uncertainty and data", "quantity", "quantity", "space and shape", "space and shape", "space and shape", "space and shape", "change and relationships", "space and shape", "space and shape", "quantity", "change and relationships", "change and relationships"), V3 = c("Constructed Response Expert", "Constructed Response Manual", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Auto-coded", "Constructed Response Expert", "Constructed Response Expert", "Constructed Response Expert", "Complex Multiple Choice", "Complex Multiple Choice", "Complex Multiple Choice", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Expert", "Complex Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice", "Complex Multiple Choice", "Simple Multiple Choice", "Constructed Response Manual", "Constructed Response Manual", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Auto-coded", "Constructed Response Manual", "Complex Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Complex Multiple Choice", "Simple Multiple Choice", "Constructed Response Auto-coded", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Manual", "Constructed Response Expert", "Constructed Response Manual", "Complex Multiple Choice", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Manual", "Simple Multiple Choice", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response Manual", "Constructed Response Manual", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Manual", "Simple Multiple Choice", "Constructed Response Manual", "Constructed Response Expert", "Simple Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Manual", "Simple Multiple Choice", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Manual", "Constructed Response Expert", "Complex Multiple Choice", "Complex Multiple Choice", "Constructed Response Expert", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Expert", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Expert", "Constructed Response Expert", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response Manual", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Manual", "Complex Multiple Choice", "Constructed Response Manual", "Constructed Response Manual", "Complex Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Manual", "Constructed Response Expert", "Constructed Response Manual", "Constructed Response Expert", "Simple Multiple Choice", "Constructed Response Manual", "Complex Multiple Choice" ), V4 = c("Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice")), class = "data.frame", row.names = c(NA, -109L ))
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)
CodePudding user response:
The following should work as expected. I'm using reactiveValues
to switch between the datasets. Please note the use of the source
argument.
PS: Please never nest render* functions in observers as you did in your example.
library(shiny)
library(plotly)
library(data.table)
library(DT)
df <- structure(list(V1 = c("PM00FQ01", "PM00GQ01", "PM00KQ02", "PM033Q01",
"PM034Q01T", "PM155Q01", "PM155Q02D", "PM155Q03D", "PM155Q04T", "PM192Q01T",
"PM273Q01T", "PM305Q01", "PM406Q01", "PM406Q02", "PM408Q01T", "PM411Q01",
"PM411Q02", "PM420Q01T", "PM423Q01", "PM442Q02", "PM446Q01", "PM446Q02",
"PM447Q01", "PM462Q01D", "PM464Q01T", "PM474Q01", "PM496Q01T", "PM496Q02",
"PM559Q01", "PM564Q01", "PM564Q02", "PM571Q01", "PM603Q01T", "PM800Q01",
"PM803Q01T", "PM828Q01", "PM828Q02", "PM828Q03", "PM903Q01", "PM903Q03",
"PM905Q01T", "PM905Q02", "PM906Q01", "PM906Q02", "PM909Q01", "PM909Q02",
"PM909Q03", "PM915Q01", "PM915Q02", "PM918Q01", "PM918Q02", "PM918Q05",
"PM919Q01", "PM919Q02", "PM923Q01", "PM923Q03", "PM923Q04", "PM924Q02",
"PM934Q01", "PM934Q02", "PM936Q01", "PM936Q02", "PM939Q01", "PM939Q02",
"PM942Q01", "PM942Q02", "PM942Q03", "PM943Q01", "PM943Q02", "PM948Q01",
"PM948Q02", "PM948Q03", "PM949Q01T", "PM949Q02T", "PM949Q03", "PM953Q02",
"PM953Q03", "PM953Q04D", "PM954Q01", "PM954Q02", "PM954Q04", "PM955Q01",
"PM955Q02", "PM955Q03", "PM957Q01", "PM957Q02", "PM957Q03", "PM961Q02",
"PM961Q03", "PM961Q05", "PM967Q01", "PM967Q03T", "PM982Q01", "PM982Q02",
"PM982Q03T", "PM982Q04", "PM985Q01", "PM985Q02", "PM985Q03", "PM991Q01",
"PM991Q02D", "PM992Q01", "PM992Q02", "PM992Q03", "PM995Q01", "PM995Q02",
"PM995Q03", "PM998Q02", "PM998Q04T"), V2 = c("space and shape", "space and
shape", "space and shape", "space and shape", "space and shape", "change and
relationships", "change and relationships", "change and relationships",
"change and relationships", "change and relationships", "space and shape",
"space and shape", "space and shape", "space and shape", "uncertainty and
data", "quantity", "uncertainty and data", "uncertainty and data",
"uncertainty and data", "quantity", "change and relationships", "change and
relationships", "space and shape", "space and shape", "space and shape",
"quantity", "quantity", "quantity", "quantity", "quantity", "uncertainty and
data", "change and relationships", "quantity", "quantity", "uncertainty and
data", "change and relationships", "uncertainty and data", "quantity", "change
and relationships", "change and relationships", "quantity", "quantity",
"quantity", "quantity", "quantity", "quantity", "change and relationships",
"uncertainty and data", "change and relationships", "uncertainty and data",
"uncertainty and data", "uncertainty and data", "quantity", "quantity",
"quantity", "space and shape", "change and relationships", "quantity", "space
and shape", "space and shape", "change and relationships", "change and
relationships", "uncertainty and data", "uncertainty and data", "quantity",
"change and relationships", "quantity", "change and relationships", "space and
shape", "quantity", "quantity", "quantity", "space and shape", "space and
shape", "space and shape", "uncertainty and data", "uncertainty and data",
"uncertainty and data", "change and relationships", "change and
relationships", "change and relationships", "uncertainty and data",
"uncertainty and data", "uncertainty and data", "change and relationships",
"change and relationships", "change and relationships", "change and
relationships", "change and relationships", "uncertainty and data", "space and
shape", "space and shape", "uncertainty and data", "uncertainty and data",
"uncertainty and data", "uncertainty and data", "uncertainty and data",
"quantity", "quantity", "space and shape", "space and shape", "space and
shape", "space and shape", "change and relationships", "space and shape",
"space and shape", "quantity", "change and relationships", "change and
relationships"), V3 = c("Constructed Response Expert", "Constructed Response
Manual", "Constructed Response Expert", "Simple Multiple Choice", "Constructed
Response Auto-coded", "Constructed Response Expert", "Constructed Response
Expert", "Constructed Response Expert", "Complex Multiple Choice", "Complex
Multiple Choice", "Complex Multiple Choice", "Simple Multiple Choice",
"Constructed Response Expert", "Constructed Response Expert", "Complex
Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice",
"Complex Multiple Choice", "Simple Multiple Choice", "Constructed Response
Manual", "Constructed Response Manual", "Constructed Response Expert", "Simple
Multiple Choice", "Constructed Response Expert", "Constructed Response
Auto-coded", "Constructed Response Manual", "Complex Multiple Choice",
"Constructed Response Manual", "Simple Multiple Choice", "Simple Multiple
Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Complex Multiple
Choice", "Simple Multiple Choice", "Constructed Response Auto-coded",
"Constructed Response Expert", "Constructed Response Manual", "Constructed
Response Manual", "Constructed Response Expert", "Constructed Response
Manual", "Complex Multiple Choice", "Constructed Response Expert", "Simple
Multiple Choice", "Constructed Response Expert", "Constructed Response
Manual", "Simple Multiple Choice", "Constructed Response Expert", "Simple
Multiple Choice", "Constructed Response Manual", "Simple Multiple Choice",
"Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response
Manual", "Constructed Response Manual", "Simple Multiple Choice", "Simple
Multiple Choice", "Constructed Response Expert", "Constructed Response
Manual", "Constructed Response Manual", "Simple Multiple Choice", "Constructed
Response Manual", "Constructed Response Expert", "Simple Multiple Choice",
"Simple Multiple Choice", "Simple Multiple Choice", "Constructed Response
Expert", "Constructed Response Manual", "Simple Multiple Choice", "Constructed
Response Expert", "Simple Multiple Choice", "Constructed Response Manual",
"Constructed Response Expert", "Complex Multiple Choice", "Complex Multiple
Choice", "Constructed Response Expert", "Constructed Response Expert",
"Constructed Response Manual", "Constructed Response Expert", "Constructed
Response Manual", "Constructed Response Expert", "Constructed Response
Expert", "Constructed Response Manual", "Constructed Response Expert",
"Constructed Response Expert", "Simple Multiple Choice", "Simple Multiple
Choice", "Constructed Response Manual", "Constructed Response Expert", "Simple
Multiple Choice", "Constructed Response Expert", "Constructed Response
Manual", "Complex Multiple Choice", "Constructed Response Manual",
"Constructed Response Manual", "Complex Multiple Choice", "Simple Multiple
Choice", "Simple Multiple Choice", "Simple Multiple Choice", "Constructed
Response Manual", "Simple Multiple Choice", "Constructed Response Expert",
"Constructed Response Manual", "Constructed Response Manual", "Constructed
Response Expert", "Constructed Response Manual", "Constructed Response
Expert", "Simple Multiple Choice", "Constructed Response Manual", "Complex
Multiple Choice" ), V4 = c("Male voice", "Male voice", "Male voice", "Male
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Female voice", "Female voice",
"Female voice", "Female voice", "Female voice", "Female voice", "Female
voice", "Female voice", "Female voice", "Female voice", "Female voice",
"Female voice", "Female voice", "Female voice", "Female voice", "Male/Female
voice", "Male/Female voice", "Male/Female voice", "Male/Female voice",
"Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female
voice", "Male/Female voice", "Male/Female voice", "Male/Female voice",
"Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female
voice", "Male/Female voice", "Male/Female voice", "Male/Female voice",
"Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female
voice", "Male/Female voice", "Male/Female voice", "Male/Female voice",
"Male/Female voice", "Male/Female voice", "Male/Female voice", "Male/Female
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male
voice", "Male voice", "Male voice", "Male voice", "Male voice", "Male voice",
"Male voice", "Male voice", "Male voice", "Male voice")), class =
"data.frame", row.names = c(NA, -109L ))
setDT(df)
dtnumA <- df[ , .N, by="V3"]
dtnumB <- df[ , .N, by="V2"]
ui <- fluidPage(
column(6, plotlyOutput("myPlotA")),
column(6, plotlyOutput("myPlotB")),
DTOutput("mydt")
)
server <- function(input, output, session) {
reactiveVals <- reactiveValues(myPlotA_event_data = NULL, myPlotB_event_data = NULL)
observe({
reactiveVals$myPlotB_event_data <- NULL # reset
reactiveVals$myPlotA_event_data <- event_data("plotly_click", source = "A")
})
observe({
reactiveVals$myPlotA_event_data <- NULL # reset
reactiveVals$myPlotB_event_data <- event_data("plotly_click", source = "B")
})
output$mydt <- renderDT({
if(!is.null(reactiveVals$myPlotA_event_data)){
df[V3 == reactiveVals$myPlotA_event_data$customdata]
} else if(!is.null(reactiveVals$myPlotB_event_data)){
df[V2 == reactiveVals$myPlotB_event_data$customdata]
}
})
output$myPlotA <- renderPlotly({
plot_ly(dtnumA, labels = ~V3, values = ~N, type = 'pie', customdata = ~V3, source = "A")
})
output$myPlotB <- renderPlotly({
plot_ly(dtnumB, labels = ~V2, values = ~N, type = 'pie', customdata = ~V2, source = "B")
})
}
shinyApp(ui, server)