I trying to make a plot (histogram for example) of datasets that gets added observations to when the user clicks a button. Here is a bare bone example: Adding data to array and plot histogram after each added data point / Mattias Villani | Observable.
The problem is that the data array gets updated, but is not reactive. So the notebook only works if the user runs the cell with the histogram after having clicked the button to update the data.
I understand that I am doing something weird here, probably against the reactive philosophy of Observable, but I hope that you can excuse a newbie.
Hope you have the time with some follow up questions, to understand the solution, and the etiquette for how to behave in this forum:
I can see that the (counter, plotObject) syntax does two things: i) since counter is now in this cell, the plot become reactive to changes in the counter, ii) it returns the last of two object in the parentheses, which displays the plot. Where can I read about the (counter, plotObject) syntax? Seems like a tuple for me, but is not, and why does it return only the second argument? Is this is hack/shortcut to get reactivity for my setting, or is it part of the observable/javascript philosophy?
Is there a better way to achieve my goal? I am fine with this solution, but I would like to know if this was a āhackā to solve my problem?
Etiquette: I saw that you forked my notebook in order to answer my question, which is a great boon of Observable. Will you keep this fork for posteriority, and if so, for how long? Should I keep my notebook so that people can dig back to my original question?
Thanks a lot for your help, and sorry for my questions, but I have been really impressed by Observable and I want to learn this well. It will revolutionize my teaching.
If youāre coming from a Python background, then I can see how it would look like a tuple. Youāre correct that itās not a tuple but it is quite standard Javascript. More precisely, according to the MDN docs:
The comma (,) operator evaluates each of its operands (from left to right) and returns the value of the last operand.
I definitely would not call this a hack; itās quite a standard way to force one short cell to depend upon another. I guess I might more typically write:
This would certainly be preferred if you require more complicated code to produce the output. I just happened to go for the shorter route this time.
Yes. Iāve got a whole collection of forum responses.
I donāt think thereās really an established etiquette surrounding this, though.
Iām happy happy to answer questions when I can but, like most folks, my time is kinda variable. And I agree about Observable - itās really quite awesome!