Reputation: 175
I want to show a multi bar plot in shiny, using the highcharter library (this is done).
I want also to show a table, which have the data in the plot.
My question is if it is possible to click on a bar and automatically subset the data, and show only the data which refers to the chosen column, by using shiny/R commands?
Here is my code:
library("shiny")
library("highcharter")
ui <- fluidPage(
column(width = 8, highchartOutput("hcontainer",height = "500px"))
)
server = function(input, output) {
output$hcontainer <- renderHighchart({
hc <- highchart() %>%
hc_xAxis(categories = a$b) %>%
hc_add_serie(name = "c", data = a$c) %>%
hc_add_serie(name = "d", data = a$d) %>%
hc_add_serie(name = "e", data = a$e) %>%
hc_plotOptions(series = list(stacking = FALSE)) %>%
hc_chart(type = "column")
hc
})
}
shinyApp(ui = ui, server = server)
I want to subset the data frame "a" by doing a[,column_x], where column_x is the selected bar in the highchart ("c", "d" or "e").
My question is how can I send the column chosen variable to the server.r?
In response to K. Rohde
Can you tell me why is this not working when I do:
myClickFunc <- JS("function(event) {Shiny.onInputChange('hcClicked', { x: this.name, y: this.categories });}")
and:
output$tablecontainer <- renderText({ paste( input$hcClicked$x,input$hcClicked$y) })
Is just printing input$hcClicked$x
?
I just need to capture the following actions: - click in a bar: which name (this is ok) and which category - click select the name in the legend - click in the plot background
Is this possible? Many thanks
Upvotes: 2
Views: 1933
Reputation: 9676
Would this be okay?
library("shiny")
library("highcharter")
ui <- shinyUI(
fluidPage(
column(width = 8, highchartOutput("hcontainer", height = "500px")),
column(width = 4, dataTableOutput("tablecontainer"))
)
)
server <- function(input, output) {
a <- data.frame(b = LETTERS[1:10], c = 11:20, d = 21:30, e = 31:40)
output$hcontainer <- renderHighchart({
myClickFunc <- JS("function(event) {Shiny.onInputChange('hcClicked', this.name);}")
highchart() %>%
hc_xAxis(categories = a$b) %>%
hc_add_serie(name = "c", data = a$c) %>%
hc_add_serie(name = "d", data = a$d) %>%
hc_add_serie(name = "e", data = a$e) %>%
hc_plotOptions(series = list(stacking = FALSE, events = list(click = myClickFunc))) %>%
hc_chart(type = "column")
})
output$tablecontainer <- renderDataTable({
if(!is.null(input$hcClicked)){
subset(a, , c("b", input$hcClicked))
}
})
}
shinyApp(ui = ui, server = server)
You can insert any JavaScript function for click events on the graph. I'd suggest further reading here to find out which values you also can extract from the click. In this example, I just sent the name of the clicked series to input$hcClicked
via shiny.
Upvotes: 2