JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
601 stars 81 forks source link

Upgrade to echarts 5.0.0 #245

Closed psimm closed 3 years ago

psimm commented 3 years ago

Hi John,

echarts 5.0.0 was just released and it has a boatload of new features and fixes, including a number of breaking changes against 4.9.

Do you plan to update echarts4r to the new version?

JohnCoene commented 3 years ago

Hi Paul,

I want to do that eventually but there are a number of other issues I need to take into account.

The first commit I made to echarts4r was in March 2018: 40a942f2569d69c43d6882699c47ba7dd13a54a8. Since then my R skills and knowledge have much improved (thankfully) and there are a lot of things I think should be changed in echarts4r.

The source code of the package is really not great, I was happy about it two years ago but it's really sub-par. The way groups are treated, the way it repeatedly breaks the DRY principle, the lack of orthogonality, poor tidyeval, and much more.

Moreover, echarts4r also supports numerous plugins (mapbox, leaflet, wordcloud, graph/network, etc.) which will likely break in version 5.

I think this warrants a new package with cleaner source code easier to maintain and contribute to. The API exposed to the user of the package should probably be changed too. I think taking inspiration from the grammar of graphics as implemented by ggplot2 would be more than welcome.

In brief, yes, I want to update to echarts version 5 but will do so in a new package.

psimm commented 3 years ago

Yes, a clean start makes a lot of sense when making a big version upgrade. I'm so glad to hear that you have further plans for echarts!

Though I'd also like to let you know that despite some rough edges and occasional bugs, echarts4r is a beloved and very useful package. I think that it's currently the best R package for interactive graphics. ggvis is not developed anymore, highcharter is proprietary and plotly lacks the polished animations and hover effects of echarts.

I agree that taking inspiration from the grammar of graphics is a good idea. It lets users build on their existing knowledge of ggplot2 and generally provides a powerful framework to construct any visualization.

JohnCoene commented 3 years ago

Maybe @etiennebacher, @helgasoft, and @swsoyee have some thoughts on next version too?

etiennebacher commented 3 years ago

I don't have much to add to what has already been said:

Apart from that, I would be very happy to contribute to a new package for echarts 5.0. I am just wondering if the fact that the codebase has been migrated to TypeScript would change anything in the structure of the R package. Not that it would change anything for me (I have almost zero knowledge in JS or TS) but just wondering.

helgasoft commented 3 years ago

Agree with @psimm - I think echarts4r is currently the most complete charting library for R. The show must go on! John, your work is impressive and highly appreciated. Every software has growing pains, a normal evolution. My suggestion would be to follow the KISS (keep-it-simple, sailor) and DRY (don't-repeat-yourself) principles. Once the basics are covered and stable, and wider adoption is achieved, then one could get fancy and spend time adding 'smart' functions. Let not forget resources too. Echarts.js has probably tens or hundreds of contributors, while here the participation is still ...hm. So just to keep both libraries in sync is already a challenge.

helgasoft commented 3 years ago

Want to experiment? Each line below is a step, follow them in order

cars %>% e_chart(speed) %>% e_bar(dist)    # v.4.x.x is in color RED
detach("package:echarts4r", unload = TRUE)    # unload so that the .js file could be replaced
# get JS v.5.0.1 from here: https://cdn.jsdelivr.net/npm/echarts@5.0.1/dist/echarts.min.js   - a 951 KB file
.libPaths()   # in [1] is the path to your R libraries
# copy echarts.min.js in folder <yourPath>\R\win-library\4.0\echarts4r\htmlwidgets\lib\echarts-4.8.0
# in the same folder there is already a file echarts-en.min.js which is v.4.8.0 (or v.4.9.0) with size ~768 KB
# rename echarts-en.min.js to echarts.4.js, then rename echarts.min.js to echarts-en.min.js
library(echarts4r)     # to load v.5.0.1
cars %>% e_chart(speed) %>% e_bar(dist)    # v.5.0.1 is in color BLUE

Now you can run your scripts with the latest version of Echarts.js.
Please post any interesting findings here 😊

EDIT: it breaks GL for sure. Tried with the latest echarts-gl.min.js v.2.0.0, but still jammed. Well, even their own examples do not work quite yet, so obviously it's evolving, not yet stable.

helgasoft commented 3 years ago

Nothing is as bad or as good as it seems... The v.2.0.0 GL surface is a white ghost, but this GL bar example shows pretty well.

# example from help - e_bar_3d {echarts4r}
volcano %>%
    as.table() %>%
    as.data.frame() %>%
    dplyr::mutate(
        Var1 = as.integer(Var1),
        Var2 = as.integer(Var2)
    ) %>%
    e_charts(Var1) %>%
    e_bar_3d(Var2, Freq) %>%
    e_visual_map(Freq)

volcano5

Here is a handy DOS batch file for fast version switching. To be saved in and used from folder "...\echarts4r\htmlwidgets\lib\echarts-4.8.0". Of course file names must be set accordingly.

echo OFF
REM run like "echaVersion v4" (or v5)
IF [%1]==[] (
 echo Version value missing: v4 or v5
 EXIT /B
)

IF [%1]==[v5] (
 :: switching to v.5
 xcopy echarts-en.min.5.0.1.js echarts-en.min.js /y
 xcopy echarts-gl.min.2.0.0.js echarts-gl.min.js /y
) ELSE (   
 :: switching to v.4
 xcopy echarts-en.min.4.9.0.js echarts-en.min.js /y
 xcopy echarts-gl.min.1.x.x.js echarts-gl.min.js /y
)
echo switched to %1 ----- done.
JohnCoene commented 3 years ago

In my tests it broke all the geo-related Viz: maps, globes, etc. :(

helgasoft commented 3 years ago

There are a few more JS files to consider in folder echarts-4.8.0 - world, ecStat, dataTool, plugins. Needs further investigation.

helgasoft commented 3 years ago

Switched just one more file to newer version - world.js. Checked the following examples - all good:

?e_map #------------------------------------------
json <- jsonlite::read_json("https://echarts.apache.org/examples/data/asset/geo/USA.json")
USArrests %>%
  dplyr::mutate(states = row.names(.)) %>%
  e_charts(states) %>%
  e_map_register("USA", json) %>%
  e_map(Murder, map = "USA") %>%
  e_visual_map(Murder)

?e_map_3d  #------------------------------------------
choropleth <- data.frame(
  countries = c(
    "France", "Brazil", "China", "Russia", "Canada", "India", "United States",
    "Argentina", "Australia"
  ),
  values = round(runif(9, 10, 25))
)
choropleth %>%
  e_charts(countries) %>%
  e_map_3d(values, shading = "lambert") %>%
  e_visual_map(min = 10, max = 30)

?e_scatter_3d  #------------------------------------------
# remotes::install_github("JohnCoene/echarts4r.assets")
library(echarts4r.assets)
airports <- read.csv(
  paste0("https://raw.githubusercontent.com/plotly/datasets/",
         "master/2011_february_us_airport_traffic.csv")
)
airports %>% 
  e_charts(long) %>% 
  e_globe(
    environment = ea_asset("starfield"),
    base_texture = ea_asset("world"), 
    globeOuterRadius = 100
  ) %>% 
  e_scatter_3d(lat, cnt, coord_system = "globe", blendMode = 'lighter') %>% 
  e_visual_map(inRange = list(symbolSize = c(1, 10)))

The globe example had 2 missing parts. Once fixed it ran beautifully.

?e_globe  #------------------------------------------
url <- paste0(
  "https://echarts.apache.org/examples/",
  "data-gl/asset/data/population.json"
)
data <- jsonlite::fromJSON(url)
data <- as.data.frame(data)
names(data) <- c("lon", "lat", "value")
data %>%
  e_charts(lon) %>%
  e_globe( 
    base_texture = ea_asset("world"),  # this line is missing in example
    displacementScale = 0.04
  ) %>%
  e_bar_3d(lat, value, coord_system="globe") %>%  # coord_system is missing in example
  e_visual_map(show = FALSE)

So far, so good. If anyone has problems, please post your code. 🤙

pissang commented 3 years ago

Hi, I'm the author of echarts-gl. Most of the compatible issues in echarts-gl including transparent bug in the surface chart have been fixed in the latest v2.0.1. Please let me known if there are other bugs I didn't notice.

JohnCoene commented 3 years ago

Thanks everyone for your inputs! I've just bumped the package to use version 5: some issues that will need fixing but it mostly works rather well! Let me know your thoughts or any issue that you may spot (open issues).