Subset a dataframe based on a plotly on-click event











up vote
1
down vote

favorite
1












I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"). The same logic is needed for all the bars.



library(plotly)

ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")

)

server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)


output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))


ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})



}

shinyApp(ui, server)









share|improve this question


















  • 1




    Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
    – Masoud
    Nov 8 at 18:49










  • Big and with deadline thanks mate!
    – firmo23
    Nov 8 at 18:50















up vote
1
down vote

favorite
1












I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"). The same logic is needed for all the bars.



library(plotly)

ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")

)

server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)


output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))


ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})



}

shinyApp(ui, server)









share|improve this question


















  • 1




    Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
    – Masoud
    Nov 8 at 18:49










  • Big and with deadline thanks mate!
    – firmo23
    Nov 8 at 18:50













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"). The same logic is needed for all the bars.



library(plotly)

ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")

)

server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)


output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))


ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})



}

shinyApp(ui, server)









share|improve this question













I have the shiny app below which displays a stacked bar chart with five bars. This bar chart is dependent on the FinalDF dataframe. What I would like to do is when I click on -lets say- the 1st bar "TC" to generate a table below with the info only related to "TC" (FinalDF$Name=="TC"). The same logic is needed for all the bars.



library(plotly)

ui <- fluidPage(
plotlyOutput("plot"),
dataTableOutput("click")

)

server <- function(input, output) {
Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
FinalDF<-data.frame(Name, Class,count)


output$plot <- renderPlotly({
# Create the stacked bar plot using ggplot()
stackedBarPlot<- ggplot(data = FinalDF,key=key) +
geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
color = "black", position = position_fill(reverse = T)) +
geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black",
mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
coord_flip() +
scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))


ggplotly(stackedBarPlot)
})
output$click <- renderDataTable({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
})



}

shinyApp(ui, server)






r shiny plotly






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 18:47









firmo23

628113




628113








  • 1




    Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
    – Masoud
    Nov 8 at 18:49










  • Big and with deadline thanks mate!
    – firmo23
    Nov 8 at 18:50














  • 1




    Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
    – Masoud
    Nov 8 at 18:49










  • Big and with deadline thanks mate!
    – firmo23
    Nov 8 at 18:50








1




1




Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49




Seems you have a big project and lots of questions. Good luck to you mate. (unnecessary comment, just trying to send good vibes tho).
– Masoud
Nov 8 at 18:49












Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50




Big and with deadline thanks mate!
– firmo23
Nov 8 at 18:50

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214258%2fsubset-a-dataframe-based-on-a-plotly-on-click-event%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214258%2fsubset-a-dataframe-based-on-a-plotly-on-click-event%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini