Add the following line in your script myfile.R:
myfile.R
source("renv/activate.R") # rest of the code
Then run: Rscript myfile.R
Rscript myfile.R