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 change/update the data within a table using only the JavaScript API #278

Closed
michaelpaulhirsch opened this issue Oct 19, 2022 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@michaelpaulhirsch
Copy link

michaelpaulhirsch commented Oct 19, 2022

Firstly, thanks for a fantastic package! It is really excellent.

I would like for a table to change its content in a quarto document with ojs, depending on an input selection that chooses from different api endpoints (all datasets have the same structure, columns, etc.)

I have been trying to find a way of doing this without using crosstalk (the tables are very large, hence the api). Is it possible to accomplish this? - changing the table's state using JavaScript only?

Thanks

@glin
Copy link
Owner

glin commented Oct 30, 2022

It's not possible today, but I've been thinking about adding this for a long time. The only issue before was that it was very unlikely to ever be used, considering everyone would already be using Shiny for dynamic data fetching/updating anyway. But now that Quarto is here, I can see more use cases for updating data via JavaScript, or even rendering the table through JavaScript alone.

One question though - do you have your data in row or column format? By row format, I mean an array of objects like [{ i: 0, x: 'a' }, { i: 1, x: 'b' }]. By column format, I mean an object of arrays like { i: [0, 1], x: ['a', 'b'] }. Most JavaScript table APIs use row format, which is typically easier to work with, but reactable internally uses column format for efficiency. I'm wondering whether reactable should support both formats, but I guess there's also HTMLWidgets.dataframeToD3() to transform columns to rows.

@glin glin added the enhancement New feature or request label Oct 30, 2022
@michaelpaulhirsch
Copy link
Author

Thanks for the reply

Yeah, the application I want to use it in is in quarto. I would be happy with either format really

Thanks

@michaelpaulhirsch
Copy link
Author

Happy to help with a PR on this, however I could only really help on the R side I'm afraid

@glin
Copy link
Owner

glin commented Nov 7, 2022

This is now done in 858cc8b:

I ended up adding support for either row or column formatted data, and it's autodetected by Reactable.setData(). Here's a short example of filtering a dataset using Observable inputs, based on Quarto's penguins example: https://github.com/glin/reactable/blob/main/vignettes/quarto-penguins/quarto-penguins.qmd. Or live at https://glin.quarto.pub/reactable-penguins/

You still need to create the table in R, but if you don't have the initial data, you can use an empty data frame and call Reactable.setData() on it afterwards, like:

data <- data.frame(species = character(0), island = character(0), bill_length = numeric(0))

reactable(data, elementId = "tbl")

If you have any fancier examples of Quarto/Observable, that would be useful too, as I've only spent a couple minutes reading about Observable and copying examples 🙂

@glin glin closed this as completed Nov 7, 2022
@glin glin added this to the v0.4.0 milestone Nov 7, 2022
@michaelpaulhirsch
Copy link
Author

thank you!!

@glin
Copy link
Owner

glin commented Nov 8, 2022

Another small update: I moved the Quarto example to vignettes/quarto/observable-reactable.qmd and added a bit more content. There's now also an example of using reactable to filter Observable charts/outputs using the new Reactable.onStateChange() method in the JS API.

Demo: https://glin.quarto.pub/observable-reactable/ (this will eventually be added to the package docs)

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

No branches or pull requests

2 participants