The library offers a range of helpful services. All of our appointments are free of charge and confidential.
The RStudio interface has four main sections: the Source window (top left), the Environment / History / Connections / Tutorial window (top right), the Console / Terminal / Jobs window (bottom left), and the Files / Plots / Packages / Help / Viewer window (bottom right).
The sizes of these windows are adjustable if you drag and drop the edges. If you are missing a window, it may be minimized (click the boxes in the top right of the section to expand / collapse certain windows). If you are missing the source window, you may need to open a file or create a new file by clicking the white sheet with the green plus sign (top left symbol on the toolbar).
The source window, normally located at the top-left of RStudio, is where you will likely do most of your work in RStudio. Here, you can open or write code / scripts, run code / scripts, view data, and more. This window is text-based only. To run code in the source window, highlight the line(s) you wish to run and either click “Run” (top right of the source window) or press CTRL + ENTER.
The environment window, normally located at the top-right of RStudio, displays which objects are currently available for computation in your workspace. Datasets and variables you import or generate will be displayed here. To view this data in the source window, you can double-click on it or run the line View(NAMEOFDATA)
(where “NAMEOFDATA” is what you named your dataset when you imported it / created it).
This window also includes tabs for history, connections, and tutorial. The history tab keeps a record of all code that has been run this session, included computations in the console. The connections tab is for accessing external databases. The tutorial tab includes more information for learning R / RStudio.
The console window, normally located at the bottom-left of RStudio, is where you can type code that you do not wish to save. For example, if you type 10 + 10 in the console and press ENTER, the console will output “20” as the answer. This is also where the code that you run in the source window might generate output.
If you see a blue greater than (>) symbol, this means the console is ready and you can run code. If you instead see a blue plus sign (+) and / or if there is no symbol in the console and / or it the top right of the console shows a red stop sign, this indicates that the console is not ready and you cannot run additional code. Either your previous code has broken in some way (you may need to restart your session), or your previous code is still running (you can force it to stop by clicking on the stop sign).
The files window, normally located at the bottom-right of RStudio, shows you the files and folder in the current directory. It’s important to set your working directory properly each session to ensure your code runs as expected (i.e., import, export; for help on setting your working directory, check the Importing Data section).
This window also includes tabs for plots, packages, help, and viewer. The plots tab is where graphs will generate if you run code to generate graphs. The packages tab allows you to see which packages are currently loaded. The help tab links to the R Documentation, and you can search functions, packages, databases, and more for additional support. The viewer tab is for dynamic data visualizations / websites generated with R.
In addition to files saved in R format (.R), you can open many kinds of text files in R, including excel text files, comma-delimited text files, and text files from other software packages (e.g., SPSS, SAS, Stata).
Before you import a file, be sure to set your working directory so R / RStudio knows where to look for the file. There are several ways to do this, but one of the simplest is to click the Session tab, click Set Working Directory, click Choose Directory. You can now navigate through your computer folders to the folder where you would like to import / export data, then click “Open”. When you set your working directory, the console will generate the line of code used, likely setwd(“C:/PATH/”) (where “PATH” is the pathway on your computer). Note you may need to change your back-slashes (\) to forward-slashes (/). If you wish to verify your current working directory, run the code getwd().
The recommended way to open a file in RStudio is to write code in the Source Window (top left). This is the recommended way so you may save your code (i.e., this keeps a record of your steps for yourself / others, so you know precisely which file at which location was used). As with most tasks, there are numerous ways to open files in R. You may wish to try these methods:
One basic way to open a file in RStudio is to use the tabs on the ribbon. You may do so as follows:
Another way to open a file in RStudio is to use the Files Window (bottom right). You may do so as follows:
Often you may wish to use functions written by others. The first step to do this is to install the package you wish to use; you must install the package the first time you wish to use the package. To install a package, run the line install.packages(“NAMEOFPACKAGE”) (where “NAMEOFPACKAGE” is the package you wish to install, e.g., “tidyverse”). The next step is to load the package you wish to use in the current session; you must load the package each time you start a new session if you wish to use the package. To load a package, run the line library(NAMEOFPACKAGE) (where “NAMEOFPACKAGE” is the package you wish to load, e.g., “tidyverse”).
It’s easy to create new variables in R. If you already have a dataframe (i.e., if you’ve already imported a file), you can add a new column using the assignment NAMEIT$NEWCOLUMN <- CODE (where NAMEIT is the name of the dataframe you imported, NEWCOLUMN is the name of the column you wish to create, and CODE is the code you would like to run. You can use this to compute values based on numeric transformations of other variables:
As an example, in the code below the “dataset.csv” file has been opened. You can see this stored as “df” in the global environment (top right). A new column has been created in this dataframe, called “mean_fakedata”, which is the average of three columns of data in the df dataframe.
We can view the new column by double-clicking on “df” in the global environment, or by typing the code View(df).
IMPORTANT NOTE: many of the changes you make to the dataframe will ONLY change the dataframe, not the original file. If you want a copy of the final file (i.e., with any changes you’ve done in R), you will need to save it to your computer. This is simple to do with write.csv(DATAFRAMENAME, “PATH/FILENAME.csv”), where DATAFRAMENAME is what you have called your dataframe, PATH is the pathway on your computer, and FILENAME is what you would like the file to be called. Saving the file as a .csv means it is easy to open in other software. In this screenshot, PATH was not specified because the working directory was already set.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.