When you knit an Rmd document the code gets executed in a clean environment other than the one you are currently working on, so you need to include in the Rmd file the code to import or define your data as well. Otherwise is going to work when run interactively (in your current environment) but not when knitted (on a clean environment).
If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.