Skip to content

R and RStudio

magnesium2400 edited this page Jun 22, 2023 · 3 revisions

The purpose of this document is to give a basic rundown of how to use R and RStudio. I will write about:

  • Intro to RStudio, and how to load a .csv file
  • How to download and load packages in RStudio
  • How to run a basic for loop in RStudio

This document is not intended to showcase all of RStudio's capabilities, or to help you with writing out code for your specific analysis type per se; RStudio has MANY freely available packages for different analysis types.

Downloading RStudio

On MASSIVE

To use RStudio locally, you need to download both R and RStudio. RStudio has already been downloaded on MASSIVE. You can load RStudio on MASSIVE by opening terminal and calling:

module load rstudio
vglrun rstudio

Or you can load it from the applications tab.

On your computer

To use RStudio locally, you need to download both R and RStudio. You can download RStudio through the Anaconda Navigator if you are frequently using applications available through that software, but I personally have had issues with using RStudio there (as have others) – so if I were you, I would download RStudio directly from CRAN instead of through Anaconda.

How to load a csv file

Once RStudio has opened, go to File > New File > R Script. This will open a new file from which you can run code. You can load a csv file by typing the following in the R Script window:

my_data <- read.csv("/home/tconstab1/kg98_scratch/Toby/demo_data.csv")

Then highlight the code and run it by clicking on ‘Run Code’ in the tab above. The <- here is like an equal sign (like in Python). The csv file will thus be saved in RStudio as a dataframe named ‘my_data.’ if you are running this code outside of a demo context (i.e. a thesis), name this dataframe something useful. You want your paper trail to be easily understood by other people who you collaborate with/other people who will be checking your code later on down the track.

How to download and load packages in RStudio

Let’s say you want to download and use ROBEX (a toolbox for ‘robust brain extraction’; https://github.com/muschellij2/robex). If you are downloading ROBEX on MASSIVE, first set up a utilities folder in the directory where you wish it to be downloaded. To download ROBEX, we first need to download a package from CRAN that allows us to install packages from Github:

install.packages('devtools',lib='/home/tconstab1/kg98_scratch/Toby/utils/R_Packages')

Notice that I have set the ‘lib’ path to my utils folder. We can then load the package by typing and running;

library('devtools',lib='/home/tconstab1/kg98_scratch/Toby/utils/R_Packages')

To download ROBEX, then write and run

devtools::install_github("muschellij2/robex",lib='/home/tconstab1/kg98_scratch/Toby/utils/R_Packages')

We can then load the package by typing and running

library(‘robex’,lib='/home/tconstab1/kg98_scratch/Toby/utils/R_Packages')

How to run a basic for loop

For loops are great for running the same analysis over a range of different values or subjects. For loops follow this general syntax pattern:

if (test_expression) {
statement
}

For example, lets say you have a list of numbers; we will call this list ‘i’. And for each of those numbers in that list, we want to add 2 IF that number is greater than 3. This is how we would structure this for loop:

i <- c(1,2,3,4,5)
for (x in i) {
	if (x>3) {
		print(x+2)
	}
}

What output do you get? We can see 2 was only added to 4 and 5. The rest of the numbers in the sequence were ignored. There are two pairs of {}, because we have an if statement embedded in the for loop - and both require a { to open the command and a } to finish it.

Let’s say you have a bunch of files you have already done a brain extraction for (using ROBEX). You will want to skip subjects you have already done an extraction for (save computational time):

subjects <- array(as.character(c(101,102,103,104,105,106,107,108)))

It is important that you annotate your code as you go. This is incredibly important – you may revisit the code in 6 months’ time and not remember what you were doing – annotating will save you. To annotate, begin a line with the # symbol; all characters after the # will be ignored by RStudio, and what you write will not pollute your coding space.

#I have 8 of my participants who I want to do a brain extraction of above; e.g. subject101 – subject108. 
#I have converted each of the elements in this array to characters so R can go looking for their files as in below:

for (subject in subjects){

    #First, I will load each of the participant’s raw nifti images into _RStudio_. 
	#I am using the readnii function from the oronifti package to load the nifty files. 
	#I am using paste0 to concatenate the file paths with each subject (e.g. sub101, then sub102) in the list ‘subjects’ (101,102,103, etc)

    raw_nifti = readnii(paste0("/home/tconstab1/kg98_scratch/Toby/MBBP /raw/",subject,"_raw /mri/adni.nii"))

	#We will then run `robex ` to perform the skull stripping for each of the nifti files

	extracted_raw_nifti = robex(raw_nifti)

   	#I then specify the file path of where I want the output to be save
	output_directory=paste0 (paste0("/home/tconstab1/kg98_scratch/Toby/MBBP /robex/",subject,"_robex /”subject”robex.nii"))

    #I then save the robex’d image to the output directory
	writeNIfTI(extracted_raw_nifti, output_directory, gzipped=TRUE, onefile=TRUE)

} 
#DO NOT FORGET TO END WITH } !!

More info

More on for loops and conditionals in R: How to Use If-Else Statements and Loops in R – Dataquest

There are a few online courses for using RStudio for psych and neuroimaging stats (incl for producing graphs / other visuals). Here are a few: