Getting Started
Running R Code in RStudio's Script Window
When running R code in the script window of RStudio (usually the top left pane), start by putting your cursor on the line of code you want to run and then either pressing the 'Run' button in the upper right corner of this pane, or by pressing Ctrl+Enter on a Windows computer, or Cmd+Enter on a Mac computer. This will push the code down to the console window (usually the bottom left pane), and show you the code and the associated output. By writing and running your code in the script window, you can save your code (as a .R file), and use it in the future.
Running R Code in RStudio's Console Window
When running R code in the console window of RStudio (usually the bottom left pane), simply type your code next to the prompt symbol, '>' and press Enter.
If the prompt symbol is shown, it means that R is ready to run another command. If there is a '+', it means that R is still waiting for something (usually from you), so it can finish running the R code.
But once you exit out of RStudio, the console is cleared. All of the code you typed and ran in the console will longer be available to you. If you want to save the code you have written, it is best to write it in the script window and save it as a .R file.
Comments in R
When a '#' is inserted on a line of code, it is called a comment. Everything after the '#' until the end of the line will not be run by R, and is interpreted as a personal note for the programmer, you!
Running R Code with the DataCamp Tutorial Feature
Here on the Stats Stuff website, we have the ability to use an R Tutorial feature provided by DataCamp. It works very similarly to running R code in the script window of RStudio. Type whatever code you would like into the script.R pane. When you are ready to run your code, press the Run button.
# no pec
# Calculate 3 + 4
3 + 4
# Calculate 6 + 12
Notice that this DataCamp feature runs all of the code you typed in the script.R pane at once rather than line by line, like in RStudio.
The dot next to the Run button tells you whether this feature is ready to run your code. If it is gray, the feature will just take a few seconds and set up your workspace before running your code. After that, it should turn green which means you will have no delay in running further R code. If it is a color other than gray or green, check your internet connection.