Skip to content

Instantly share code, notes, and snippets.

@helgasoft
helgasoft / shiny.resize.R
Created October 2, 2021 03:09
R | ECharts | dynamic resize with echarty
# https://echarts.apache.org/en/api.html#echartsInstance.resize
# https://echarts.apache.org/examples/en/editor.html?c=line-easing
library(shiny); library(dplyr); library(echarty)
ui <- fluidPage( titlePanel("Resize with echarty"),
fluidRow(ecs.output('chart')),
checkboxInput('toggle900','Toggle Size 900px'),
checkboxInput('toggle400','Toggle Size 400px')
)
@helgasoft
helgasoft / brush.dispatch.R
Last active August 9, 2022 00:16
R | ECharts | brush action dispatch
library(shiny)
library(echarty)
ui <- fluidPage(
fluidRow(
column(8, ecs.output("chart")),
column(4, actionButton("bzoom", "Brush"), #verbatimTextOutput('verb'))
tableOutput('dats') )
)
)
@helgasoft
helgasoft / ecStat.regression.R
Last active December 3, 2021 01:20
R | ECharts | regression lines on data groups
# draw regression lines on data groups with echarty
# see https://echarts.apache.org/en/option.html#dataset.fromDatasetIndex
# see https://echarts.apache.org/en/option.html#series-line.datasetIndex
library(echarty); library(dplyr)
linecolor <- c('blue','green','brown') # color of regression lines
p <- iris %>% group_by(Species) %>%
ec.init(js = 'echarts.registerTransform(ecStat.transform.regression);') # load ecStat
# add transformations and regression lines to preset scatter groups
@helgasoft
helgasoft / init.locale.R
Last active August 9, 2022 21:25
R | ECharts | custom or built-in locale; Graphic text and image
# set a custom locale in ECharts with echarty
# idea from @williamorim
# install.packages("remotes")
remotes::install_github("helgasoft/echarty")
library(echarty)
library(lubridate)
df <- data.frame(date=as.Date('2019-12-31') %m+% months(1:13), num=runif(13))
@helgasoft
helgasoft / shiny.module.echarty.R
Last active September 27, 2021 06:47
R | ECharts | Shiny | modules and data selection
# original code by https://github.com/Camil88
library(shiny)
library(dplyr)
library(nycflights13)
library(shinyWidgets)
library(echarty)
library(scales)
toggle <- FALSE
@helgasoft
helgasoft / boxplot.R
Last active March 26, 2022 00:43
R | ECharts | boxplot
library(echarty); library(dplyr)
# 1) boxplot with calculation in R ---------------------
p <- ec.init()
p$x$opts$series <- list(
list(type='boxplot', name='mpg', data=list(boxplot.stats(mtcars$mpg)$stats)),
list(type='boxplot', name='hp', data=list(boxplot.stats(mtcars$hp)$stats)),
list(type='boxplot', name='disp',data=list(boxplot.stats(mtcars$disp)$stats))
)
@helgasoft
helgasoft / endLabel.R
Last active May 24, 2022 11:57
R | ECharts | use ec.clmn in endLabel formatter and in pictorialBar symbol
library(echarty); library(dplyr)
# set first column as X and second as Y to avoid explicitly define them later
mydf <- data.frame(
Date = c("2021-08-02", "2021-08-03", "2021-08-04", "2021-08-05", "2021-08-06", "2021-08-09", "2021-08-10", "2021-08-11", "2021-08-02", "2021-08-03", "2021-08-04", "2021-08-05", "2021-08-06", "2021-08-09", "2021-08-10", "2021-08-11", "2021-08-02", "2021-08-03", "2021-08-04", "2021-08-05", "2021-08-06", "2021-08-09", "2021-08-10", "2021-08-11", "2021-08-02", "2021-08-03", "2021-08-04", "2021-08-05", "2021-08-06", "2021-08-09", "2021-08-10", "2021-08-11"),
CasesPer100K = c(397, 575, 579, 591, 617, 733, 654, 690, 54, 79, 83, 88, 92, 76, 104, 107, 84, 89, 95, 101, 106, 123, 125, 133, 151, 249, 287, 249, 259, 245, 371, 378),
State = c("Florida", "Florida", "Florida", "Florida", "Florida", "Florida", "Florida", "Florida", "Massachusetts", "Massachusetts", "Massachusetts", "Massachusetts", "Massachusetts", "Massachusetts", "Massachusetts", "Massachusetts", "New York", "New York", "Ne
@helgasoft
helgasoft / gauge.colors
Last active July 29, 2021 18:27
R | Echarts | gauge undocumented
# Undocumented feature found for 'gauge' in ECharts (https://echarts.apache.org/en/option.html#series-gauge.type)
# Attribute 'encode' is not even mentioned in the docs, but could be used when 'dataset' is present.
# All columns listed in 'encode' (like x,y,z) are ignored and first remaining dataset column becomes gauge data.
# In example below this 'first remaining' column is 'circumference'. Neat!?
library(echarty); library(dplyr)
df <- Orange %>% filter(age==1582) %>% mutate(Tree=as.character(Tree))
clr <- c("#eeb422","#382278","#d9534f",'magenta','red')
p <- df %>% group_by(Tree) %>% ec.init( preset=FALSE,
@helgasoft
helgasoft / area.bands.R
Last active January 3, 2022 03:44
R | ECharts | area bands (confidence bands)
#' Area band (confidence bands) is a 'custom' serie with lower and upper boundaries
#' When type='polygon', coordinates of the two boundaries are chained into a polygon and displayed as one.
#' When type='stack', two (smooth) stacked lines are drawn, one with customizable areaStyle.
#' The upper boundary coordinates should be values added on top of the lower boundary coordinates.
#' Standard and custom(formatter) tooltips could be used.
#'
#' ec.init(load='custom') will preset values like dataset, xAxis, etc.
#' those could be customized (like xAxis below), and new ones added - like legend and tooltip.
library(echarty)
@helgasoft
helgasoft / shiny.bind2
Last active May 19, 2023 14:19
Binding two ECharts without connect
library(shiny)
library(tidyverse)
library(echarty)
ui <- fluidPage(
HTML("Binding two charts on <b>inside zoom</b> only<br />
Tooltips could be bound too, by `axis`<br />
Alternative to ECharts <a href='https://echarts.apache.org/en/api.html#echarts.connect'>connect</a><br />
Solution adapted from <a href='https://github.com/apache/echarts/blob/12f6620c25eb96faa3e9f9fbc13a076c3c8bdbbc/test/connect-manually.html'>this code</a>"),
tags$head(tags$script(HTML("