Closed lyndonbird closed 4 years ago
Hello! You're welcome! Have you a minimal example ? I'll investigate to see what's the problem
Victor
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)
I've also solved the second part, I had closing brackets in the wrong place!
Thanks anyway, keep the updates coming!
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:
By setting apexcharter's set_input_click() I'm able to retrieve the donut segment that is selected by the user. I'm however struggling to work out how to determine when the segment is unselected by a user. The click isn't firing an update to the input value i.e input$click <-NA or NULL
With the charts setup with the reactive data, I've got it setup to change the data being passed to the apexchart object (incl. the series colours). I however cannot seem to figure out how to get the legend to refresh. I've tried the config_update options with no luck.
I'm not sure if I'm overlooking something with these two things, any help would be appreciated!