What is R?
-
R is a powerful opensource programming language suited to mathematical and statistical computing, analysis and graphics.
- Download R from r-project.org.
-
RStudio provides a convenient integrated development environment (IDE) or interface for using R.
- Download RStudio (Desktop Open Source Edition) from posit.co.
RStudio Cloud provides R and RStudio via a web browser. You can make a free account but the amount of time you can use the service for free is limited, and you must pay for usage beyond the limit. The cloud version may be an option if you have difficulties setting up the software on your own computer. Remember to copy any files you make from RStudio Cloud to your own computer so that you do not risk losing your work. RStudio Cloud can be found at posit.cloud.
Both R and RStudio (Desktop Open Source Edition) are free.
We will use R via RStudio in my courses to gather, describe, visualise and analyse financial data. We use Quarto to create reproducible research and analysis reports. In Portfolio Management, we analyse securities risk and returns, construct investment portfolios, and model investment signals. In Financial Data Workshop and Financial Econometrics (Graduate), we build econometric models of securities returns and analyse corporate finance data. In my seminar, students conduct their research and write their graduation paper following reproducible research principles.
For my courses, please install R and RStudio on your laptop and bring it to the classes in which we are scheduled to work on data analysis. Please ensure you can connect to the internet via the university WIFI or other means.
This guide shows screenshots for R/RStudio on MacOS. The Windows and Linux versions of R/RStudio are similar.
Opening RStudio for the first time
- After you have installed R and RStudio, open RStudio. It should look something like this.
Set a default working directory for RStudio
- From the Tools menu, open the RStudio’s Global Options.
From “General” set the default working directory to your documents or another convenient folder on your hard drive. You can also use some cloud services for your R files like Dropbox, however Google Drive and Microsoft Onedrive can cause problems.
-
The working directory tells R where to look for your files. The default directory you select will be where R looks for a file when you are not working in a “project”. Next we will create a project.
- I set my default working directory to the syncd_r_data older on my Dropbox.
After you have set the default working directory, quit and reopen RStudio.
Projects in RStudio
A project allows you to keep data, code and documents for a particular task together. Projects help you to keep your files organised.
A project also allows you to customise the setup of R for the task or analysis you are performing.
-
You may have several different projects and and customise the way R is set up differently for each project.
- I create a project for each course that I teach and each research project that I’m working on.
- If you use R for several classes, your seminar or other purposes, you can create a project for each of these.
Each project will have its own folder, and separate files to save the data in your environment, your history of code executed in the console, and the way you have R configured.
You can see that at present, I do not have a project open as the menu title appears as “Project: (none)”. When you are working in a project, the title of the menu will include the project name.
Let’s make a new project from the Projects menu at the top-right of the screen. The projects menu allows you to create a new project, open an existing project, and contains a list of recent projects.
- To create a new project in a new folder, click “New Directory”.