dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

Reactive Chart, Legend not refreshing #12

Closed lyndonbird closed 4 years ago

lyndonbird commented 4 years ago

Hi,

I'll start by saying thanks for your work creating the R wrapper for apexcharts.

I have two donut charts that display different perspectives of the same reactive data i.e Owner & Type. I'm trying to make them interactive with each other so that when one is selected it filters the other and vise versa. I'm using reactive values to filter the dataset being used by the charts.

The problem I'm having is two fold:

I'm not sure if I'm overlooking something with these two things, any help would be appreciated!

pvictor commented 4 years ago

Hello! You're welcome! Have you a minimal example ? I'll investigate to see what's the problem

Victor

lyndonbird commented 4 years ago

I managed to solve the first part, I had to set the ObserveEvent to IgnoreNull = FALSE.

For the second part with the legend not refreshing, here is an example -

library(dplyr)
library(apexcharter)
library(shiny)

df <- data.frame(Company= c('Company A', 'Company B', 'Company C', 'Company A', 'Company D','Company C'),
                Sector = c('Tech', 'Primary Foods', 'Manufacturing','Primary Foods','Tech', 'Utilities'),
                Revenue = c(48392, 109039, 59302, 14902, 86739, 38920)
)

companyColours <- data.frame(Company = c('Company A', 'Company B', 'Company C', 'Company D'),
                             colour = c("#5cbae5", "#b6d957", "#fac364", "#9ea8ad")
)

sectorColours <- data.frame(Sector = c('Primary Foods', 'Manufacturing', 'Tech', 'Utilities'),
                             colour = c("#F26968", "#DF6E21", "#EFAA52", "#56B1BF")
)

shinyui <- fluidPage(
           fluidRow(apexchartOutput('companyPie')),
           fluidRow(apexchartOutput('sectorPie'))
)

server <- function(input, output, session) {

  pieSelected = reactiveValues(sector = NA, company = NA)

  observeEvent(input$piecompanyclick,{
    if(is.null(input$piecompanyclick)){
      pieSelected$company <- NA 
    }else if(is.na(pieSelected$company)==T | pieSelected$company != input$piecompanyclick){
      pieSelected$company <- input$piecompanyclick
      pieSelected$sector <- NA
    } else {
      pieSelected$company <- NA 
    }
  },ignoreNULL = FALSE)

  observeEvent(input$piesectorclick,{
    if(is.null(input$piesectorclick)){
      pieSelected$sector <- NA 
    }else if(is.na(pieSelected$sector)==T | pieSelected$sector != input$piesectorclick){
      pieSelected$sector <- input$piesectorclick
      pieSelected$company <- NA
    } else {
      pieSelected$sector <- NA 
    }
  },ignoreNULL = FALSE)

  output$sectorPie <- renderApexchart({
    req(df)

    df_sector <- data.frame(df %>% 
                               filter(if(is.na(pieSelected$company)) TRUE else (Company==pieSelected$company)) %>% 
                               select(Sector, Revenue) %>% 
                               group_by(Sector)  %>% 
                               summarize(Revenue=sum(Revenue))
                            )
    print(df_sector)

    colourPalette <- t(merge(data.frame(Sector=df_sector$Sector),sectorColours, by='Sector') %>% select(colour))

    a <- apex(data = df_sector, type = "donut", mapping = aes(x = Sector, y = Revenue, fill = Sector, auto_update = config_update(update_options = TRUE,
                                                                                                                                  options_redrawPaths = TRUE,
                                                                                                                                  series_animate =TRUE,
                                                                                                                                  options_animate = TRUE))) %>% 
      ax_colors(colourPalette) %>%
      ax_title("% by Sector",floating= F)  %>% 
      ax_tooltip(enabled = TRUE, 
                 x = list(show=TRUE), 
                 y=list(formatter= format_num(",.2f",suffix=' MWh'))) %>%
      set_input_click("piesectorclick")

  })

  output$companyPie <- renderApexchart({
    req(df)

    df_company <- data.frame(df %>% 
                               filter(if(is.na(pieSelected$sector)) TRUE else (Sector==pieSelected$sector)) %>% 
                               select(Company, Revenue) %>% 
                               group_by(Company)  %>% 
                               summarize(Revenue=sum(Revenue))
    )
    print(df_company)

    colourPalette <- t(merge(data.frame(Company=df_company$Company),companyColours, by='Company') %>% select(colour))

    a <- apex(data = df_company, type = "donut", mapping = aes(x = Company, y = Revenue, fill = Company, auto_update = config_update(update_options = TRUE,
                                                                                                                                     options_redrawPaths = TRUE,
                                                                                                                                     series_animate =TRUE,
                                                                                                                                     options_animate = TRUE))) %>% 
      ax_colors(colourPalette) %>%
      ax_title("% by Company",floating= F)  %>% 
      ax_tooltip(enabled = TRUE, 
                 x = list(show=TRUE), 
                 y=list(formatter= format_num(",.2f"))) %>%
      set_input_click("piecompanyclick")

  })
}

shinyApp(ui = shinyui, server = server)
lyndonbird commented 4 years ago

I've also solved the second part, I had closing brackets in the wrong place!

Thanks anyway, keep the updates coming!