The scene that you are asking about appears in chapter 7 of Stephen Crane's Red Badge of Courage.
It is important to note that Henry did not run during his first encounter with the enemy. He held fast and did his job as a soldier; however, the enemy regrouped quite quickly and made another surge at Henry's position. At this point, his imagination got the better of him and Henry didn't see the enemy as men, but as a machine that could not be stopped. That's when he turned and ran away.
At the start of chapter 7, Henry learns that his comrades were successful in repelling the second attack. Henry is a bit angry at them for staying and attributes their success to luck. In his opinion he did the rational thing because the odds were against success and by retreating he guaranteed his survival in order to fight another day. He also rationalizes his flight when he sees a squirrel run away from him because it felt threatened. Henry reasons that if nature's response is flight when it feels threatened, then he himself was only doing the natural thing when his own life was in jeopardy.