Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to convert the R code back to JS? #510

Closed
lillianlu-nz opened this issue Mar 26, 2023 · 3 comments
Closed

Is there a way to convert the R code back to JS? #510

lillianlu-nz opened this issue Mar 26, 2023 · 3 comments

Comments

@lillianlu-nz
Copy link

lillianlu-nz commented Mar 26, 2023

Hi there,

I'm an R user and love the echarts4r package. However, I was recently asked whether I can provide the code in JS so the graph can be embedded on our website easily. I don't know much JavaScript so it'd be difficult to write it from scratch.

Is there a way/function to convert what I've created in R into the code like this: https://echarts.apache.org/examples/en/editor.html?c=area-basic

I imagine it'd be similar to running the shiny widgets in RStudio console to get the HTML code output.

I tried the jsonlite package but it didn't work. No luck with Chat GPT too...

Thank you so much!

@JohnCoene
Copy link
Owner

You can retrieve the JSON

https://echarts4r.john-coene.com/reference/echartsnjson?q=Json#null

Let me know if that worked

@lillianlu-nz
Copy link
Author

lillianlu-nz commented Mar 26, 2023

Thank you for your prompt response! It worked well. It adds quotation marks to all the properties though, so I did some wrangling to get rid of them.

# Convert R code to JSON

json <- plot |>
e_inspect(
json = TRUE,
pretty = TRUE
)

# Get rid of "" around properties

json_output <- gsub('"(\w+)"\s*:', '\1:', json)

# Get rid of "" around formatter functions

json_output <- gsub('"(?=.*function)|(?<=;})"', '', json_output, perl = TRUE)

@JohnCoene
Copy link
Owner

Technically JSON should have these, you can leave them in its valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants