dim(X) must have a positive length

Happy to help if you can provide some data (just make dummy data if you need to with the right properties). A reprex (FAQ: How to do a minimal reproducible example ( reprex ) for beginners) makes it much easier for us to help you.