I cannot find the cause of the error when I use this function. You can find a minimal example here.

Thanks in advance