When RStudio first opens with three window panes, the R console on the left side of the window, and two window panes on the right side of the window.
Default RStudio window pane configuration.
When using RStudio in practice, open a fourth window pane. To do so,
click on the green + button at the top-left of the RStudio
window. Clicking on this window results in the following drop-down menu,
which reveals that RStudio can process many types of documents,
including Python and SQL documents, and, of course, R documents.
Choose R Script from the drop-down menu.
Choose the R Script option, listed first. The result is
a window pane for entering text, specifically intended for R code.
The fourth RStudio window pane for R code, which appears in the top-left of the window.
Now that the Source window pane is open, enter some R code. Click the
Run button right above the code to run the selected lines
of code. RStudio copies the code, enters the code into the console, and
then runs the code.
Some code in the Source window.
What if you do enter code directly into the R console? Here the
head(d) function call was entered directly into the console
instead of the R Source window.
The head() function entered directly into the console.
Not a problem. Note the History tab int he top-right
window pane. All R instructions entered into the console, either
directly or indirectly through the Source window pane, are stored in the
History tab.
RStudio History window pane.
Click on the Source button in the History
tab after selecting the newly entered R instruction. It is then copied
to the Source window pane.
The To Source option.
Of course at some point do save your file of R code for later use as needed. Here the file was saved as eg.R. Now click the Compile Report button, which looks kind of like a tin can.
A line of History transferred to the Source window.
Then choose your output media, including MS Word. (To get a pdf directly you may need additional software. Instead go to HTML output and then export/print the web page to PDF from your browser.)
Choose your form of output of your analysis.
Much easier to get MS Word output than copying and pasting.