Sourcing a script has to do with having R running several commands either one after another with stopping. In order to do this R Studio you need to type your code into Source Editor which is normally in the top left-hand side of the program. We are going to go through an example in which R will ask a question and you will answer it.
In the source editor, type the following information. At the end of each line press enter to move to the next line. I will explain the meaning of the text after the example
h <- "Welcome to R" yourname <- readline("What is your name?") print(paste(h, yourname))
After typing this information, you should click on the source button near the top middle of the computer screen. If this is done correctly you should see the following in the console.
> source('~/.active-rstudio-document') What is your name?
Type your name in response to the question and press enter. Then you should see the following.
> source('~/.active-rstudio-document') What is your name?Darrin  "Welcome to R Darrin"
So what exactly did we do?
- We made a variable named h and assigned the object “Welcome to R”
- Next, we made a variable called yourname and assigned the function readline to read the phrase “What is your name?”. The readline function literally reads text.
- We then told R to print a combination (using the paste function) of the h variable first (Welcome to R) and the yourname variable second (What is your name?)
- Next we clicked the source script button
- In the console, we were asked the question “What is your name?”
- We responded with our name.
- R takes your name and combines it with the h variable to create the following phrase
- “Welcome to R Darrin”
Several steps of code was run at once through using the script editor. This same process could have been done in the console but using the script editor saves time. Many aspects of programming focus on saving time and improving efficiency. There are always several ways to do something but the goal is always to find the method that takes the least amount of effort and improves the readability of the code.