Before we start
Last updated on 2024-03-12 | Edit this page
What is R? What is RStudio?
The term “R
” is used to refer to both the programming
language and the software that interprets the scripts written using
it.
RStudio is a popular way to write R scripts and interact with the R software. To function correctly, RStudio needs R and therefore both need to be installed on your computer.
Why learn R?
R does not involve lots of pointing and clicking, and that’s a good thing
In R, the results of your analysis rely on a series of written commands, and not on remembering a succession of pointing and clicking. If you want to redo your analysis because you collected more data, you can rerun the script and R will process the new dataset exactly the same way as before.
Working with scripts makes the steps you used in your analysis clear, and the code you write can be inspected by someone else who can give you feedback and spot mistakes.
R code is great for reproducibility
Reproducibility is when someone else, including your future self, can obtain the same results from the same dataset by running the same analysis.
R can also be integrated with other tools to generate manuscripts from your code. If you collect more data, or fix a mistake in your dataset, the figures and the statistical tests in your manuscript can be updated automatically.
R is widely used in academia and in industries such as pharma and biotech where analyses are sometimes expected to be reproducible, so knowing R will give you an edge.
R is interdisciplinary and extensible
With 10,000+ packages that can be installed to extend its capabilities, R provides a framework that allows you to combine statistical approaches from many scientific disciplines to best suit the analytical framework you need to analyze your data. For instance, R has packages for image analysis, GIS, time series, population genetics, and a lot more.
R works on data of all shapes and sizes
The skills you learn with R scale easily with the size of your dataset. Whether your dataset has hundreds or millions of lines, it won’t make much difference to you. R can connect to spreadsheets, databases, and many other data formats, on your computer or on the web.
R produces high-quality graphics
The plotting functionalities in R are endless, and allow you to adjust any aspect of your graph to visualize your data more effectively.
R has a large and welcoming community
Thousands of people use R daily. Many of them are willing to help you
through mailing lists and websites such as Stack Overflow, RStudio community, and Slack
channels such as
the R for Data Science online community (https://www.rfordatasci.com/).
In addition, there are numerous online and in person meetups organised
globally through organisations such as R Ladies Global (https://rladies.org/).
Finding your way around RStudio
Let’s start by learning about RStudio, which is an Integrated Development Environment (IDE) for working with R.
We will use RStudio IDE to write code, navigate the files on our computer, inspect the variables we are going to create, and visualize the plots we will generate. RStudio can also be used for many other useful things (e.g., version control, developing packages, writing Shiny apps) that we will not cover during the workshop.
RStudio is divided into 4 “panes”:
- The Source pane for your scripts and documents (top-left, in the default layout)
- The Environment/History pane (top-right) which shows all the objects in your working space (Environment), and your command history (History)
- The Files/Plots/Packages/Help/Viewer pane (bottom-right)
- The R Console (bottom-left)
The placement of these panes and their content can be customized (see menu, Tools -> Global Options -> Pane Layout). For ease of use, settings such as background color, font color, font size, and zoom level can also be adjusted in this menu (Global Options -> Appearance).
One of the advantages of using RStudio is that all the information you need to write code is available in a single window. Additionally, with many shortcuts, autocompletion, and highlighting for the major file types you use while developing in R, RStudio will make typing easier and less error-prone.
Project-based organisation
It is good practice to keep a set of related data, analyses, and text self-contained in a single project folder, and when programming this is called the working directory. Any scripts inside the project folder can then use relative paths to point to files and folders within the working directory. Working this way allows you to move your project around on your computer and share it with others while still allowing the scripts to work. This is opposed to using absolute paths, which point to where a file is on a specific computer, but which will break scripts that are being run on another machine.
RStudio provides a helpful set of tools for owrking with projects through it’s “Projects” interface, including creating a working directory for you, but also remembering its location (allowing you to quickly navigate to it) and optionally preserves custom settings and (re-)open files to resume work after a break. To create an “RStudio Project” for this tutorial:
- Start RStudio.
- Under the
File
menu, click onNew Project
. ChooseNew Directory
, thenNew Project
. - Enter a name for this new folder (or “directory”), and choose a
convenient location for it. This will be your working
directory for the rest of the day (e.g.,
~/Desktop/data-carpentry
). - Click on
Create Project
. - Download the code
handout, place it in your working directory and rename it (e.g.,
data-carpentry-script.R
). - (Recommended) Set Preferences to ‘Never’ save workspace in RStudio.
A workspace is your current working environment in R which includes any user-defined objects. By default, all of these objects will be saved, and automatically loaded, when you reopen your project. This can be cumbersome, and have unintentional consequences, especially if you are working with larger datasets, and it can lead to hard to debug errors by having objects in memory you forgot you had. Therefore, it is often a good idea to turn this off. To do so, go to Tools –> ‘Global Options’ and select the ‘Never’ option for ‘Save workspace to .RData’ on exit.’
Organizing your working directory
Using a consistent folder structure across your different research projects will help to keep things organized, and will help you to find things in the future. This can be especially helpful when you have multiple projects on the go at once. In general, we recommend creating directories (folders) for scripts, data, figures and documents.
-
data_raw/
&data/
Use these folders to store raw data and intermediate datasets you may create for the need of a particular analysis. You should always keep a copy of your raw data accessible and do as much of your data cleanup and preprocessing programmatically (i.e., with scripts, rather than manually) as possible. Separating raw data from processed data is a good idea. -
documents/
This would be a place to keep outlines, drafts, and other text relating to manuscripts or notes. -
fig/
This is where you can save any plots or figures. -
scripts/
This would be the location to keep your R scripts for different analyses or plotting, and potentially a separate folder for your functions (more on that later). - Additional (sub)directories depending on your project needs.
For this workshop, we will need a data_raw/
folder to
store our raw data, and we will use data/
for when we learn
how to export data as CSV files, and a fig/
folder for the
figures that we will save.
- Under the
Files
tab on the right of the screen, click onNew Folder
and create a folder nameddata_raw
within your newly created working directory (e.g.,~/data-carpentry/
). (Alternatively, typedir.create("data_raw")
at your R console.) Repeat these operations to create adata
andfig
folder.
We are going to keep the script in the root of our working directory because we are only going to use one file. Later, when you start to create more complex projects, it might make sense to organize scripts in sub-directories.
Your working directory should now look like this:
The working directory
The working directory is an important concept to understand. It is the place from where R will be looking for and saving the files. When you write code for your project, it should refer to files in relation to the root of your working directory and only need files within this structure.
RStudio assists you in this regard and sets the working directory
automatically to the directory where you have placed your project in. If
you need to check it, you can use getwd()
. If for some
reason your working directory is not what it should be, you can change
it in the RStudio interface by navigating in the file browser where your
working directory should be, and clicking on the blue gear icon “More”,
and select “Set As Working Directory”. Alternatively you can use
setwd("/path/to/working/directory")
to reset your working
directory. However, your scripts should not include this line because it
will fail on someone else’s computer.
Interacting with R
The basis of programming is that we write down instructions for the computer to follow, and then we tell the computer to follow those instructions. We write, or code, instructions in R because it is a common language that both the computer and we can understand. We call the instructions commands and we tell the computer to follow the instructions by executing (also called running) those commands.
There are two main ways of interacting with R: by using the console
or by using script files (plain text files that contain your code). The
console pane (in RStudio, the bottom left panel) is the place where
commands written in the R language can be typed and executed immediately
by the computer. It is also where the results will be shown for commands
that have been executed. You can type commands directly into the console
and press Enter
to execute those commands, but they will be
forgotten when you close the session.
Because we want our code and workflow to be reproducible, it is better to type the commands we want in the script editor, and save the script. This way, there is a complete record of what we did, and anyone (including our future selves!) can easily replicate the results on their computer.
RStudio allows you to execute commands directly from the script
editor by using the Ctrl
+
Enter
shortcut (on Macs,
Cmd
+ Return
will work,
too). The command on the current line in the script (indicated by the
cursor) or all of the commands in the currently selected text will be
sent to the console and executed when you press
Ctrl
+ Enter
. You can
find other keyboard shortcuts in this RStudio
cheatsheet about the RStudio IDE.
At some point in your analysis you may want to check the content of a
variable or the structure of an object, without necessarily keeping a
record of it in your script. You can type these commands and execute
them directly in the console. RStudio provides the
Ctrl
+ 1
and
Ctrl
+ 2
shortcuts allow
you to jump between the script and the console panes.
If R is ready to accept commands, the R console shows a
>
prompt. If it receives a command (by typing,
copy-pasting or sent from the script editor using
Ctrl
+ Enter
), R will
try to execute it, and when ready, will show the results and come back
with a new >
prompt to wait for new commands.
If R is still waiting for you to enter more data because it isn’t
complete yet, the console will show a +
prompt. It means
that you haven’t finished entering a complete command. This is because
you have not ‘closed’ a parenthesis or quotation, i.e. you don’t have
the same number of left-parentheses as right-parentheses, or the same
number of opening and closing quotation marks. When this happens, and
you thought you finished typing your command, click inside the console
window and press Esc
; this will cancel the
incomplete command and return you to the >
prompt.
Seeking help
Searching function documentation with ?
and
??
If you need help with a specific function, let’s say
mean()
, you can type ?mean
or press
F1 while your cursor is on the function name. If you are
looking for a function to do a particular task, but don’t know the
function name, you can use the double question mark ??
, for
example ??kruskall
. Both commands will open matching help
files in RStudio’s help panel in the lower right corner. You can also
use the help panel to search help directly, as seen in the
screenshot.
Automatic code completion
When you write code in RStudio, you can use its automatic code completion to remind yourself of a function’s name or arguments. Start typing the function name and pay attention to the suggestions that pop up. Use the up and down arrow to select a suggested code completion and Tab to apply it. You can also use code completion to complete function’s argument names, object, names and file names. It even works if you don’t get the spelling 100% correct.
Package vignettes and cheat sheets
In addition to the documentation for individual functions, many
packages have vignettes – instructions for how to use the
package to do certain tasks. Vignettes are great for learning by
example. Vignettes are accessible via the package help and by using the
function browseVignettes()
.
There is also a Help menu at the top of the RStudio window, that has cheat sheets for popular packages, RStudio keyboard shortcuts, and more.
Finding more functions and packages
RStudio’s help only searches the packages that you have installed on your machine, but there are many more available on CRAN and GitHub. To search across all available R packages, you can use the website rdocumentation.org. Often, a generic Google or internet search “R <task>” will send you to the appropriate package documentation or a forum where someone else has already asked your question. Many packages also have websites with additional help, tutorials, news and more (for example tidyverse.org).
Dealing with error messages
Don’t get discouraged if your code doesn’t run immediately! Error messages are common when programming, and fixing errors is part of any programmer’s daily work. Often, the problem is a small typo in a variable name or a missing parenthesis. Watch for the red x’s next to your code in RStudio. These may provide helpful hints about the source of the problem.
If you can’t fix an error yourself, start by googling it. Some error messages are too generic to diagnose a problem (e.g. “subscript out of bounds”). In that case it might help to include the name of the function or package you’re using in your query.
Asking for help
If your Google search is unsuccessful, you may want to ask other R users for help:
- Stack Overflow: Many questions have already been answered, but the challenge is to use the right words in your search to find them. If your question hasn’t been answered before and is well crafted, chances are you will get an answer in less than 5 min. Remember to follow their guidelines on how to ask a good question.
- The R 4 Data Science Slack Community: is a fantastic place to get answers to specific questions that you can’t find answers to with Google or Stack Exchange. Make sure you are at least familiar with the material in R 4 Data Science before asking a question here.
The key to receiving help from someone is for them to rapidly grasp your problem. Thus, you should be as precise as possible when describing your problem and help others to pinpoint where the issue might be. Try to:
- Use the correct words to describe your problem. Otherwise you might get an answer pointing to the misuse of your words rather than answering your question.
- Generalize what you are trying to do, so people outside your field can understand the question.
- Reduce what does not work to a simple reproducible example. For instance, instead of using your real data set, create a small generic one. For more information on how to write a reproducible example see this article from the reprex package. Learning how to use the reprex package is also very helpful for this.
- Include the output of
sessionInfo()
in your question. It provides information about your platform, the versions of R and the packages that you are using. As an example, here you can see the versions of R and all the packages that we are using to run the code in this lesson:
R
sessionInfo()
OUTPUT
#> R version 4.3.3 (2024-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.4 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] RSQLite_2.3.1 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0
#> [5] dplyr_1.1.2 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0
#> [9] tibble_3.2.1 ggplot2_3.4.2 tidyverse_2.0.0 knitr_1.42
#>
#> loaded via a namespace (and not attached):
#> [1] bit_4.0.5 gtable_0.3.3 compiler_4.3.3 renv_1.0.5
#> [5] highr_0.10 tidyselect_1.2.0 blob_1.2.4 scales_1.2.1
#> [9] fastmap_1.1.1 yaml_2.3.7 R6_2.5.1 generics_0.1.3
#> [13] munsell_0.5.0 DBI_1.1.3 pillar_1.9.0 tzdb_0.3.0
#> [17] rlang_1.1.1 utf8_1.2.3 cachem_1.0.8 stringi_1.7.12
#> [21] xfun_0.39 bit64_4.0.5 memoise_2.0.1 timechange_0.2.0
#> [25] cli_3.6.1 withr_2.5.0 magrittr_2.0.3 grid_4.3.3
#> [29] hms_1.1.3 lifecycle_1.0.3 vctrs_0.6.2 evaluate_0.20
#> [33] glue_1.6.2 fansi_1.0.4 colorspace_2.1-0 tools_4.3.3
#> [37] pkgconfig_2.0.3
- The rOpenSci community call “How to ask questions so they get answered”, (rOpenSci site and video recording) includes a presentation of the reprex package and of its philosophy.
- blog.Revolutionanalytics.com and this blog post by Jon Skeet have comprehensive advice on how to ask programming questions.
How to learn more after the workshop?
The material we cover during this workshop will give you a taste of how you can use R to analyze data for your own research. However, to do advanced operations such as cleaning your dataset, using statistical methods, or creating advanced graphics you will need to learn more.
A useful next step is R 4 Data Science, a free online book which starts in a similar way to this workshop, but which contains more intermediate and some advanced concepts too. There are lots of exercises to work through to really cement your understanding, so we recommend progressing through this as far as you can.
The best way to become proficient and efficient at R, as with any other tool, is to use it to address your actual research questions. As a beginner, it can feel daunting to have to write a script from scratch. But given that many people make their code available online, it can be useful to see how other’s structure their code.