As we cannot reproduce the error on our side because you did not provide a reproducible example, I can just give you hint of possible issues. Please provide a reprex if you want us to help you more easily
When you see some difference between a code running in the R session, and not running when clicking on the Knit button, you should take a look at the environment. You must have in mind that a Rmd when render with the IDE button is knitted in a new clean R session. Sometimes, there could be differences if you have some stuff in your workspace that are not yet reproduced in your Rmarkdown file. For example, if data
does not containes Gender
, or if a library is not loaded properly.
I could not be that at all but I just was i though reading your issue.
If you can provide more context on the error or a reproducible example, we could help more. Thank you !