Doing package installation in a script is a fairly brittle solution. Instead, package your scripts as an R package and then use the normal package dependency mechanisms deal with the issues. Building an R package is a very straightforward process. Then, you can do:
All necessary dependencies are installed and all code is readily available.