Download the scripts for this tutorial:
Startup
P distribution
Power
Confidence Intervals.R
Some useful resources for this topic:
Using R as a simulation tool in teaching introductory statistics
Play It Again: Teaching Statistics With Monte Carlo Simulation