how to apply the margins() function within the map() function

Can you modifiy your reprex to include your data ?

It will allow us to reproduce the error on our hand. Currently, mydataframe is unknow and I can rerun your code.