default-header
HomeA short R tutorial

A short R tutorial

Friday,  November 18, 2022

GGIR for those without prior R experience

R package GGIR has been designed with the ambition to be accessible without prior R experience. For example, usage of GGIR entails only a single function call that takes care of data loading, analyses, and report generation. The primary learning curve for new GGIR users is to become familiar with all the optional input arguments.

Nonetheless, for those with no prior R experience it can be a challenge to find your way around the R environment itself. To help you with this, we have compiled the following R tutorial tailored to what we think you need to know about R in order to get started with GGIR.

RStudio
  1. Install R and RStudio, these are two separate pieces of software which complement each other.
  2. Open RStudio.
Using an R script
  1. Go to the Toolbar -> File -> New file -> script. This will open a new empty script.
  2. Type inside the script: print(2 + 2)
  3. The text print(2 + 2) is called R code.
  4. Save the file somewhere on your computer with Toolbar -> File -> Save as…
  5. Press the Source button in the top corner of your script.
  6. Do you see a 4 in the RStudio console window?
  7. Note that in other R introduction courses you may learn to type the R commands directly in the console window. We are not doing this because our GGIR commands are going to be long and it would be impractical to type them in the console. Instead we strongly recommend you to follow the steps above and work with R scripts for everything you do with GGIR.
Moving on to characters and vectors
  1. Replace the code by: print(“hello world”)
  2. Save the script (Hint on Windows computer you can do so with Ctrl + S) and press again the Source button.
  3. Do you now see “hello world” in the RStudio console window?
  4. Replace the code by: print(c(1, 2, 3, 4, 5))
  5. Save the script and press again the Source button.
  6. Do you now see 1 2 3 4 5 in the RStudio console window?
  7. Replace the code by: print(1:5)
  8. Save the script and press again the Source button.
  9. Do you now see 1 2 3 4 5 again? This is correct, 1:5 is the same as c(1, 2, 3, 4, 5).
Using functions and looking up documentation
  1. Replace the code by: print(cor(x = 1:10, y = 2:11))
  2. Do you see a 1?
  3. You just used the R function cor. To look up what cor does, go to the console and type ?cor and press Enter.
  4. Do you now see the documentation for the cor function and can you tell what function cor calculates?
  5. As you may have noticed we are specifying an x and a y in the cor command. x and y are what we call function arguments to the function ‘cor’ and in this case the argument value specified for x is 1:10 and the argument value given for y is 2:11. In GGIR you will be working with a lot of function arguments which can have character values such as “hello world”, numeric values like 4, or numeric vector values such as c(1, 2, 3, 4, 5) or 1:5. Additionally we will be working with Boolean argument values, which can be TRUE or FALSE. Note that these have no quotes around them. For example, we will use Booleans to tell GGIR to do something or to not do something.

Specifying a file path

  1. Replace the code by file.exists(“C:/Users/vvanh/Desktop/tutorial.R”)
  2. Now edit the code such that it specifies a file that exists on your computer.
  3. Save the script and press again the Source button.
  4. Do you now see TRUE in the console? If yes, then this means you specified the file path correctly. If you see FALSE then something went wrong. Keep trying until you see TRUE. Hint: R expects forward slashes, and when specifying a file always include the file extension (.xlsx, .txt, .docx, .bin, .gt3x, .cwa, .R, .csv, etcetera) even if you Windows file browser does not display them.

Once you have mastered the above steps you should be all set to explore GGIR, either via the package vignette or via one of our GGIR training courses.

A short R tutorial
Photo: GGIR