I made an edit that might have fixed the problem!
I'm just writing code out of thin air so it's hard to get it all right. If you need more help, I recommend creating a reproducible example. FAQ: What's a reproducible example (`reprex`) and how do I create one?