Hello,
I typically place most of my code into an isolate when I use the function. I would follow a similar approach here. Since I can't run your code I can't exactly tell you where. This is why a reprex is handy FAQ: How to do a minimal reproducible example ( reprex ) for beginners