Added a check to see if there is an active R plot before trying to delete it.