I wouldn't use renv::snapshot() within a document. When working on a project, you might discover that you have to install new packages from the command line using renv::install().
Then, run your script again, and see if anything breaks, e.g. due to incompatible package versions or compiler errors.
If everything works, you know you have a constellation of packages and versions that work well together. If that's the case, call renv::snapshot() manually to store your current config in the lock file.
If not, use renv::restore() to go back to the previous working configuration, and figure out how to make things work, e.g. by checking different versions of the new package you need.