knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(echarts4r)

e_common(
    font_family = "Raleway"
)
New Features
Use (almost) any function on a dynamic proxy!

echarts4r comes with proxies (functions ending in _p) as well as the ability access selected data. You will find a demo of shiny working with echarts4r here.

Shiny Demo

Deploy

A note if you are publishing on shinyapps.io, if you have locally installed the development version of the package from Github with remotes as indicated on the homepage of the website please reinstall it devtools. Otherwise shinyapps.io will intall the CRAN version which might lead to issues.

# install.packages("devtools")
devtools::install_github("JohnCoene/echarts4r")

If you are using the Shiny Server, remember to install the package on the respective server with the following command:

# stable version
sudo su - -c "R -e \"install.packages('echarts4r', repos = 'https://cran.rstudio.com')\""
# dev version
sudo su - -c "R -e \"remotes::install_github('JohnCoene/echarts4r')\""

Events

Get data from shiny with the following:

If you want to implement a missing callback with e_distpatch_action_p.

Paul Simmering kindly created a demo app showcasing callbacks.

Callbacks demo

Proxies

Interact with the charts without redrawing (proxies):

See the example below and the various proxies documentation.

library(shiny)
library(echarts4r)

ui <- fluidPage(
  actionButton("add", "Add Data to y"),
  echarts4rOutput("plot"),
  verbatimTextOutput("selected")
)

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

  data <- data.frame(x = rnorm(10, 5, 3), y = rnorm(10, 50, 12), z = rnorm(10, 50, 5))

  react <- eventReactive(input$add, {
    set.seed(sample(1:1000, 1))
    data.frame(x = rnorm(10, 5, 2), y = rnorm(10, 50, 10))
  })

  output$plot <- renderEcharts4r({
    data |> 
     e_charts(x) |> 
     e_scatter(y) |>
     e_scatter(z) |> 
     e_brush(throttleDelay = 1000)
  })

  observeEvent(input$add, {
    echarts4rProxy("plot") |> 
      e_append1_p(0, react(), x, y)
  })

  output$selected <- renderPrint({
    input$plot_brush
  })

}

shinyApp(ui, server)

Loading

You can also show a spinner while shiny recalculates.

Without loading, chart redraws with neat animation.

# no redraw
# no loading
library(shiny)
ui <- fluidPage(
  fluidRow(
    column(12, actionButton("update", "Update"))
  ),
  fluidRow(
    column(12, echarts4rOutput("plot"))
  )
)

server <- function(input, output){
  data <- eventReactive(input$update, {
    data.frame(
      x = 1:10,
      y = rnorm(10)
    )
  })

  output$plot <- renderEcharts4r({
    data() |> 
      e_charts(x) |> 
      e_bar(y)
  })
}

shinyApp(ui, server)

With loading.

# keep UI
# add loading
server <- function(input, output){
  data <- eventReactive(input$update, {
    Sys.sleep(1) # sleep one second to show loading
    data.frame(
      x = 1:10,
      y = rnorm(10)
    )
  })

  output$plot <- renderEcharts4r({
    data() |> 
      e_charts(x) |> 
      e_bar(y) |> 
      e_show_loading()
  })
}

shinyApp(ui, server)


JohnCoene/echarts4 documentation built on Feb. 23, 2024, 9:18 a.m.