styler
packagescale()
Before we cover the R language itself, we should talk about how you should run your code and where it should live. As mentioned, R is both a programming language and an environment where you can run code written in that language. The environment is a program (confusingly also called R) that allows you to interact with it and run simple lines of code one at a time. This environment is very useful for learning how the language works and troubleshooting, but it is not suitable for recording and running large, complex analyses that require many lines of code. Therefore, all important R code should be written and saved in a file before you run it! The code may not be correct, and the interactive R environment is helpful for debugging and troubleshooting, but as soon as the code works it should be saved to the file and rerun from there.
With this in mind, the basic unit of an R analysis is the R script. An R
script is a file that contains lines of R code that run sequentially as a unit
to complete one or more tasks. Every R script file has a name, which you choose
and should be descriptive but concise about what the script does; script.R
,
do_it.R
, and
a_script_that_implements_my_very_cool_but_complicated_analysis_and_plots.R
are generally poor names for scripts, whereas analyze_gene_expression.R
might
be more suitable.
In RStudio, you can create a new script file in the current directory using the
File -> New File -> R Script
menu item or the new R Script button at the top
of the screen:
Your RStudio configuration should now enable you to write R code into the (currently unsaved) file in the top left portion of the screen (labeled in the figure as “File Editor”).
You are now nearly ready to start coding in R!