If your main goal is to save objects in this environment, you can use the knitr package with the chunk cache=TRUE option, in which case all objects in the piece are saved in files, and you can load them into R later for debugging; knitr will create a .rdx and .rdb respectively for a fragment in the cache directory, and you can call lazyLoad() to load objects in these databases into R.
If you want to do more things with the environment in which the piece is evaluated, knitr also has chunk hooks and you have access to the environments through hooks; so basically you can do whatever you want - knitr makes almost everything accessible to the user. Note that each chunk is evaluated in a different empty environment when the cache is turned on. I have no examples on this subject yet, so please let me know if my description here is clear enough.
source share