Makie @lift for a function with multiple outputs?

I’m trying to make a menu in Makie which changes the plot. Basically this:

using GLMakie

fig = Figure()
menu = Menu(fig, options = [1,2])
fig[1, 1] = vgrid!(
    Label(fig, "Index a", width = nothing),menu;
    tellheight = false, width = 200)

ax = LScene(fig[1, 2])
a = Node{Int}(1)

function my_function(a,x)
    data = [sin.(pi*x), cos.(pi*x)]
    return data[a]
xplot = LinRange(-1,1,100)
yplot = @lift my_function($a,xplot) 
plt = scatter!(ax, xplot, yplot)

on(menu.selection) do s
    a[] = s     

except that my actual my_function returns multiple outputs

function my_function(a,x)
    data = [sin.(pi*x), cos.(pi*x)]
    other_stuff = rand()
    return data[a],other_stuff

Node and @lift don’t seem to work for this setting (I get ERROR: LoadError: MethodError: no method matching iterate(::Observable{Tuple{Vector{Float64}, Float64}})). Is there a way around this?

Well, I fixed this by just rewriting the function, but I’m still not sure how to fix this in a less invasive way.

If I understand correctly, just index into the result here.

yplot = @lift my_function($a,xplot)[1]

Also you technically don’t need a, you can use the selection observable directly

yplot = @lift my_function($(menu.selection), xplot)[1]
1 Like

Thanks! So if I need other_stuff as well, I guess I’ll just index into my_function again.

And directly using $(menu.selection) works, just needed to initialize menu.selection[] = 1 beforehand.