Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active July 22, 2021 23:16
Show Gist options
  • Select an option

  • Save helgasoft/433e68af0240cff36ee63730a4e42abb to your computer and use it in GitHub Desktop.

Select an option

Save helgasoft/433e68af0240cff36ee63730a4e42abb to your computer and use it in GitHub Desktop.
selecting series in Shiny with p_replace
---
title: "Select Serie"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
#' replace chart series interactively in Shiny
#' idea from https://github.com/EKtheSage
#' RE: https://github.com/JohnCoene/echarts4r/issues/325
library(flexdashboard)
library(shiny)
library(dplyr)
library(echarty)
```
Column {.sidebar}
-----------------------------------------------------------------------
### Control
```{r}
radioButtons('yvars',
label = 'Select a Y Variable to Plot',
selected = 'Sepal.Width',
choices = c('Sepal.Width', 'Petal.Length', 'Petal.Width'))
```
Column
-----------------------------------------------------------------------
### Chart
```{r}
dset <- legend <- series <- NULL
output$chart <- ecs.render({
# add column color
tmp <- data.frame(Species = as.vector(unique(iris$Species)),
color = c("#387e78","#eeb422","#d9534f"))
df <- iris %>% inner_join(tmp)
p <- df %>% group_by(Species) %>% ec.init()
p$x$opts$xAxis <- list(name='Sepal.Length', nameLocation='center')
p$x$opts$yAxis <- list(name='Sepal.Width', nameRotate=90, nameLocation='center')
# save valuable data from presets, reuse later
dset <<- p$x$opts$dataset
legend <<- p$x$opts$legend
series <<- p$x$opts$series
p
})
observeEvent(input$yvars, {
p <- ecs.proxy('chart')
p$x$opts <- list(
xAxis = list(name='Sepal.Length', nameLocation='center'),
yAxis = list(name=input$yvars, nameRotate=90, nameLocation='center'),
dataset = dset,
legend = legend,
series = lapply(series, function(s) {
s$encode = list(y=input$yvars, x='Sepal.Length'); s })
)
p %>% ecs.exec('p_replace')
})
ecs.output("chart")
```
@helgasoft

helgasoft commented Jul 2, 2021

Copy link
Copy Markdown
Author

echarty solution. Original code from @EKtheSage.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment