R2-D2 (or Artoo-Detoo, spelled out phonetically) is an astromech droid, a type of robot that usually served as a mechanic and backup computer for small spaceships. Astromechs cannot speak; they communicate with electronic beeps through an interpreter droid or computer. The fact that R2-D2 cannot directly express himself may have helped him fly under the radar and avoid frequent memory wipes, which in turn allowed him to develop a distinct, eccentric personality.
R2-D2 in the Prequels
Sometime before 32 BBY, the droid manufacturing company Industrial Automation created R2-D2 as part of the R2 series of astromech droids. He was purchased and modified by the Royal Engineers of Naboo and served aboard Queen Amidala's Royal Starship. R2-D2's quick repairs allowed Amidala to escape during the Trade Federation's blockade of Naboo in 32 BBY. He first met his future counterpart, the protocol droid C-3PO, when the ship made an emergency landing on Tatooine.
When Padmé Amidala became a Senator, she took R2-D2 with her. She later gave the droid to her husband, Anakin Skywalker, after he became a Jedi Knight. R2-D2 served as the maintenance droid for Anakin's starfighter during much of the Clone Wars. Although it was protocol to wipe droids' memories regularly, Anakin let R2-D2 accumulate information and knowledge without a memory wipe so he would become better at his job. This nearly put the Republic at risk when R2-D2 fell into enemy hands.
After the end of the Clone Wars in 19 BBY, Obi-Wan Kenobi entrusted R2-D2 and C-3PO -- along with Anakin and Padmé's daughter Leia -- to Alderaan Senator Bail Organa. The droids were forced to escape when pirates attacked the Tantive IV and spent the next few years traveling with a succession of different masters, including speeder jockey Thall Joben and explorer Mungo Baobab.
R2-D2 in the Original Trilogy and Beyond
At some point, R2-D2 and C-3PO found their way back to the Tantive IV, where they served under Princess Leia. In 0 BBY, they accompanied Leia on a mission to Tatooine to contact Obi-Wan Kenobi. When the Empire attacked, Leia hid the plans to the Death Star, a new Imperial superweapon, inside R2-D2.
The droids escaped to the planet's surface, where they were captured by Jawas and sold to the moisture farmer Owen Lars, and his nephew, Luke Skywalker. Knowing that Obi-Wan was nearby, R2-D2 showed part of Leia's recording to Luke, enticing him to remove the restraining bolt that kept the droid from escaping. This allowed R2-D2 to escape, seeking out Obi-Wan on his own.
R2-D2 eventually reunited with Leia when Luke rescued her from the Death Star, with the help of Han Solo and Chewbacca. During the rest of the Galactic Civil War, R2-D2 primarily served as the mechanic droid for Luke's X-wing fighter. He later went with Luke to the new Jedi Academy on Yavin 4. After Luke was exiled in 43 ABY, R2-D2 left his service and returned to Leia. Passed down from generation to generation, he eventually came to serve Luke's descendant Cade Skywalker in 137 ABY.
Some droids are programmed with personalities, but others will begin to develop one if they go long enough without a memory wipe. R2-D2 avoided memory wipes since at least 19 BBY, when Anakin Skywalker owned him, and as a result became quite stubborn and eccentric. At one point, he even refused to show recordings of Anakin and Padmé -- even knowing how long Luke had been searching for the truth about his mother -- in an attempt to protect Luke and Leia.
Because R2-D2 can only communicate in beeps and whistles, however, the full extent of his personality doesn't always come across. C-3PO spends much of his time mitigating R2-D2's rude comments and dismissing his useful insights, while Luke seems to frequently humor the droid, perhaps not realizing the extent of R2-D2's personality development. One of the few places where R2-D2's speech is actually translated appears in Enemy Lines II: Rebel Stand by Aaron Allston and gets the point across fairly quickly:
"YOUR FACIAL FEATURE SET SUGGESTS THAT YOU DO NOT HAVE THE INTELLIGENCE TO RETAIN MY NAME FOR MORE THAN A NANOSECOND. IT IS EVIDENT THAT YOU HAVE BEEN TAUGHT TO REPEAT SOUNDS YOU HAVE HEARD AND THAT YOU UNDERSTAND NEITHER THE WORDS YOU HEAR NOR THE ONES THAT EMERGE FROM YOUR MOUTH."
R2-D2 Behind the Scenes
In creating the early scripts for Star Wars, George Lucas took inspiration from Japanese samurai films. R2-D2 and C-3PO were inspired by Akira Kurosawa's film The Hidden Fortress (1958), which uses two peasants as comic narrators for a historical adventure about the upper classes.
R2-D2 was portrayed by actor and comedian Kenny Baker in the Star Wars films. Lucas needed someone small to fit inside the robot and maneuver it; Baker, who is 3 feet 8 inches tall, got the part "because I was the smallest guy they'd seen up until then." A separate R2-D2 model, used for scenes when the droid is moving, was controlled via remote. About 18 different R2-D2 models appear in the Prequel Trilogy, as well as CGI for scenes of the droid flying and walking up stairs.
Sound designer Ben Burtt called creating R2-D2's voice "the toughest challenge" he faced in the Star Wars films. He eventually created a mix of electronic noises and himself speaking in baby talk. The addition of a human voice helps the emotion come across in R2-D2's expressions, even if he has no words.