How to get the previous value of the cell from which a function was called?

this is the previous value of a cell. I had hoped it was passed to the called function, but it doesn’t seem so according to https://beta.observablehq.com/d/5bbb7d8edeea576e.

I’m looking for a way to get the previous value of a cell which calls a string literal function (such as in https://beta.observablehq.com/@mbostock/graphviz), in the called function, without having to pass it as an argument.

Hey Magnus,

We don’t override this for function cells, because it has an existing meaning in the language, and because function cells are values. I think what you’re looking for is something like this fork - you can use the bind, call, and apply methods to set the this context of a function from its caller, and you can use that technique to pass the caller’s this value to the callee. Here are examples of the various ways to do that. That said, I may have misread your question - let me know if this isn’t the precise intent.

Thanks Tom.

I’m aware of the methods to set the this context from the caller. What I would like to do is to use the same syntax as @mbostock does in https://beta.observablehq.com/@mbostock/graphviz:

dot`digraph { a -> b; }`

which generates an svg element.

What I’m aiming at is to create a similar function:

 adot`digraph { a -> b; }`

or rather:

 adot`${dotSrc}`

which would use d3-graphviz to render animated transitions between graphs when dotSrc changes.

For that purpose I would need to retrieve the svg generated from the previous call to adot in the adot function itself.

I’ll cook up a notebook to show the functionality tomorrow.

Got it - that’s going to be a bit tricky to fix for the general case - one option is to do something like this - close over the div and graphviz instance so that transitions are possible. Downside, of course - and probably why you didn’t already do this - is that this wouldn’t automatically support multiple instances.

Thanks. That gives a pretty decent syntax, but, yes, I would like to support multiple instances.

Is there any other way to get a reference to the cell being evaluated? Through the call stack or similar?

@tom This is what I ended up with. Any comments are welcome.