The example you provided sounds like it could be wrapped up in a function (maybe?) and documented in a package instead of a standalone script. That's an approach that some people have found successful when building modules for shiny applications and it might have some value for you as well.